我æ£åœ¨å°†this 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
在Tic Tac Toe游æˆä¸ä¿å˜å¯èƒ½çº¿æ¡çš„(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
值å¯ä»¥æ›´æ”¹ã€‚
性能至关é‡è¦ï¼ˆè¿™å°±æ˜¯æˆ‘达到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
文件ä¸ï¼Œä¸èƒ½åŒ…å«ä»»ä½•æ ‡å¤´ã€‚ä¸ºä»€ä¹ˆï¼Ÿå› ä¸ºè¿™æ˜¯ç”¨äºŽæœºå™¨äººç«žèµ›çš„åº“çš„ç‰‡æ®µï¼Œå¹¶ä¸”é€šå¸¸åªèƒ½æ交一个文件。
ç”案 0 :(得分:2)
在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
}
å½“ç„¶ä½ å¯ä»¥åœ¨å¾ªçŽ¯ä¸å¤„ç†å®ƒ
å½“ä½ å†³å®šä½¿ç”¨ä¸ŽPython的动æ€åˆ—表ç‰ä»·çš„std Vector类时,åŒæ ·çš„方法是æ£ç¡®çš„
ç”案 1 :(得分:1)
æˆ‘æƒ³ä½ å¯ä»¥ä½¿ç”¨ 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))