如何用函数的结果初始化静态成员数组

How to initialize static member array with a result of a function?

本文关键字:初始化 静态成员 数组 结果 何用 函数      更新时间:2023-10-16

我正在把这个Python文件的片段翻译成c++:

SIDE = 3
LINES = []
for y in range(SIDE):
    row = tuple((x, y) for x in range(SIDE))
    LINES.append(row)
for x in range(SIDE):
    col = tuple((x, y) for y in range(SIDE))
    LINES.append(col)
LINES.append(tuple((x, x) for x in range(SIDE)))
LINES.append(tuple((SIDE - x - 1, x) for x in range(SIDE)))

LINES包含井字游戏中可能的线的(x, y)坐标。对于SIDE = 3,它包含:

[((0, 0), (1, 0), (2, 0)), 
 ((0, 1), (1, 1), (2, 1)), 
 ((0, 2), (1, 2), (2, 2)), 
 ((0, 0), (0, 1), (0, 2)), 
 ((1, 0), (1, 1), (1, 2)), 
 ((2, 0), (2, 1), (2, 2)), 
 ((0, 0), (1, 1), (2, 2)), 
 ((2, 0), (1, 1), (0, 2))]

SIDE值可以改变

What I've try

性能是至关重要的(这就是为什么我达到c++),所以我想计算LINES只一次。因此,我选择将LINES作为类TicTacToeState的静态成员来实现。

我以这样的代码开始:

static char init_lines() {
    return 'a';
}
class TicTacToeState {
    static char LINES;
};
char TicTacToeState::LINES = init_lines();

它的工作原理。如何改变LINES数组?也许矢量会更好?对吗?

也许静态成员不是最好的选择,也许有更简单的方法?

你怎么把它翻译成c++ ?

我们知道LINES的大小,它总是2 * SIDE + 2.

特殊要求

所有c++代码必须在一个.cpp文件中,没有头文件。为什么?因为这是一个用于机器人竞赛的库片段,而且通常只能提交一个文件。

在c++中可以使用组初始化来初始化静态数组成员

    static int a[10] = {5}; //this will initialize first position item with 5 and rest with 0s
    static char b[2] = {'b', 'b'};
    static int c[2][2] = { {1,1}, {1,2} };
    int main()
    {
        cout<< a[0] << endl; //output: 5
        cout<< a[1] << endl; //output: 0
        cout<< b[0] << endl; //output: b
        cout<< c[0][1] << endl; //output: 1
    }

尽管事实是你需要知道数组的大小不像Python的列表那样是动态的


如果需要向表中插入动态计算的值,最好的方法是创建工厂方法

    static int** fact(int width, int height)
    {
        int** a;
        a = new int*[width]; //we can do it when it is DYNAMIC array! 
        a[0] = new int[height];
        a[1] = new int[height];
        for(int i = 0; i < width; i++)
            for(int k = 0; k < height; k++)
                a[i][k] = i*k;
        return a;
    }
    static int** c = fact(2, 2); //you can call it with your SIDE var
    int main()
    {
        cout<< c[1][1] << endl; //output: 1
    }

当然你可以在循环中处理

当你决定使用std Vector类时,同样的方法也是合适的,它相当于Python的动态列表

我想你可以使用lambda函数这样做:

#include <vector>
#include <iostream>
const auto SIDE = 3U;
struct coord
{
    unsigned x;
    unsigned y;
    coord(unsigned x, unsigned y): x(x), y(y) {}
};
static const auto lines = [] // lambda function
{
    // returned data structure
    std::vector<std::vector<coord>> lines;
    for(auto y = 0U; y < SIDE; ++y)
    {
        lines.emplace_back(); // add a new line to back()
        for(auto x = 0U; x < SIDE; ++x)
            lines.back().emplace_back(x, y); // add a new coord to that line
    }
    for(auto x = 0U; x < SIDE; ++x)
    {
        lines.emplace_back();
        for(auto y = 0U; y < SIDE; ++y)
            lines.back().emplace_back(x, y);
    }
    lines.emplace_back();
    for(auto i = 0U; i < SIDE; ++i)
        lines.back().emplace_back(i, i);
    lines.emplace_back();
    for(auto i = 0U; i < SIDE; ++i)
        lines.back().emplace_back(SIDE - i - 1, i);
    return lines;
}(); // NOTE: () is important to run the lambda function
int main()
{
    for(auto const& line: lines)
    {
        std::cout << "(";
        for(auto const& coord: line)
            std::cout << "(" << coord.x << ", " << coord.y << ")";
        std::cout << ")n";
    }
}
输出:

((0, 0)(1, 0)(2, 0))
((0, 1)(1, 1)(2, 1))
((0, 2)(1, 2)(2, 2))
((0, 0)(0, 1)(0, 2))
((1, 0)(1, 1)(1, 2))
((2, 0)(2, 1)(2, 2))
((0, 0)(1, 1)(2, 2))
((2, 0)(1, 1)(0, 2))