如何用函数的结果初始化静态成员数组
How to initialize static member array with a result of a function?
我正在把这个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))
相关文章:
- C - 初始化静态成员的方法
- 如何在运行时使用静态成员函数初始化静态成员变量
- 如何在班级中声明和初始化静态成员
- 初始化静态成员使编译工作.但是为什么
- 使用作用域内生成的指针初始化静态成员
- 初始化静态成员类(用于信号处理程序)
- 可以通过成员初始化列表初始化静态成员变量
- 如何用功能指针作为参数初始化静态成员的模板
- 运行主后,是否可以初始化静态成员变量
- 在C++中初始化静态成员
- 必须初始化静态成员
- 初始化静态成员变量值的正确位置
- 初始化静态成员函数指向同名函数的指针
- 如何初始化静态成员变量"dynamically"?
- 如何用函数的结果初始化静态成员数组
- 是否可以动态初始化静态成员数组?
- 初始化静态成员时重声明错误
- 避免在初始化静态成员时使用new()
- LNK 2001:初始化静态成员时未解析的外部符号
- 如何在c++中重写=操作符时初始化静态成员