创建一个二维向量数组

3

我一直在尝试为游戏创建一个二维向量数组。以下是我的示例代码:

struct TILE {
    int a;
    char b;
    bool c;
};

TILE temp_tile;

std::vector<TILE> temp_vec_tile;
std::vector<std::vector<TILE>> tile;


for (int x = 0; x < 10; x++) {
    for (int y = 0; y < 10; y++) {

    temp_tile.a = x;
    temp_tile.b = "a";
    temp_tile.c = false;;

    temp_vec_tile.push_back(temp_tile);
    }

    tile.push_back(temp_vec_tile);
}

// Why does this not work?
int x = tile[3][5].a;

注意:我不想使用Boost来完成这个任务。
谢谢。

Brian R. Bondy已经解决了这个问题。 - user382909
1个回答

0

你每次都没有清空内部向量。可能你想要的是将内部向量声明放在第一个for循环内部。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接