开发者

creating a 2D array of vectors

开发者 https://www.devze.com 2023-01-05 10:38 出处:网络
I have been trying to create a 2D array of vector for a game. Here is an example of what I am doing. struct TILE {

I have been trying to create a 2D array of vector for a game. Here is an example of what I am doing.

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

TILE temp_t开发者_JS百科ile;

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;

Note: I don't want to use Boost for this.

Thanks


You are not clearing the internal vector each time. Probably what you want is to put the internal vector declaration inside of the first for loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消