gpt4 book ai didi

c++ - 创建一个二维 vector 数组

转载 作者:太空狗 更新时间:2023-10-29 23:20:11 25 4
gpt4 key购买 nike

我一直在尝试为游戏创建一个二维 vector 数组。这是我正在做的一个例子。

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。

谢谢

最佳答案

您并不是每次都清除内部 vector 。可能您想要的是将内部 vector 声明放在第一个 for 循环中。

关于c++ - 创建一个二维 vector 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3173500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com