如何使用函数的结果åˆå§‹åŒ–é™æ€æˆå‘˜æ•°ç»„?

时间:2015-10-14 23:37:39

标签: c++ arrays static initialization

我正在将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文件中,ä¸èƒ½åŒ…å«ä»»ä½•æ ‡å¤´ã€‚为什么?因为这是用于机器人竞赛的库的片段,并且通常åªèƒ½æ交一个文件。

2 个答案:

答案 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))