Creating a two-dimensional array of vectors

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

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;

Note. I do not want to use Boost for this.

thank

+3
source share
1 answer

You do not clear the inner vector every time. You probably want to place the internal vector declaration inside the first loop.

0
source

Source: https://habr.com/ru/post/1753018/


All Articles