简体   繁体   English

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

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

I'm translating such fragment of this Python file to C++: 我正在将这个 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 holds (x, y) coordinates of possible lines in Tic Tac Toe game. LINES在Tic Tac Toe游戏中保存可能线条的(x,y)坐标。 So for SIDE = 3 it holds: 因此,对于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 value can change. SIDE值可以改变。

What I've tried 我试过的

Performance is crucial (that's why I reached for C++), so I would like to calculate LINES only once. 性能至关重要(这就是我达到C ++的原因),所以我想只计算一次LINES Thus, I've chosen to implement LINES as a static member of the class TicTacToeState . 因此,我选择将LINES实现为类TicTacToeState的静态成员。

I started with such code: 我开始使用这样的代码:

static char init_lines() {
    return 'a';
}

class TicTacToeState {
    static char LINES;
};

char TicTacToeState::LINES = init_lines();

It works. 有用。 How to change LINES to an array? 如何将LINES更改为数组? Maybe vector will be better? 也许矢量会更好? With pairs? 配对?

Maybe static member is not the best choice, maybe there is an easier way? 也许静态成员不是最好的选择,也许有一个更简单的方法?

How would you translate it to C++? 你会如何将它翻译成C ++?

We know the size of LINES , it's always 2 * SIDE + 2. 我们知道LINES的大小,它总是2 * SIDE + 2.

Special requirement 特殊要求

All C++ code must be in one .cpp file, no headers. 所有C ++代码必须在一个.cpp文件中,没有标题。 Why? 为什么? Because this is fragment of a library for bot competitions and it's typical that you can submit only one file. 因为这是用于机器人竞赛的库的片段,并且通常您只能提交一个文件。

In C++ you can initialize static array members using group initialization 在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
    }

Although the fact is you need to know size of the array not like in Python's list that are dynamically 虽然事实是你需要知道数组的大小,而不是像动态的Python列表中那样


If you need to insert to the table values calculated dynamically the best way to do this is to create factory method 如果需要插入动态计算的表值,最好的方法是创建工厂方法

    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
    }

Of course you can process it in loops 当然你可以循环处理它

The same approach will be proper when you will decide to use std Vector class which is equvalent of Python's dynamic list 当你决定使用与Python的动态列表等价的std Vector类时,同样的方法也是正确的

I suppose you could do this using a lambda function like this: 我想你可以使用像这样的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";
    }
}

Output: 输出:

((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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM