What is the correct way to insert columns into a 2D pointer vector?

I use a 2D array as the board for the board game I am making. Elements are pointers to objects of type "Tile". Indexes are x and y, which indicate horizontal and vertical position, respectively. In the Board class, it looks like this.

vector<vector<Tile*> > playGround; 

The problem is that as the game progresses, tiles are added to playGround, and the vector should be able to expand if this requires a new tile position. I am trying to add rows at the top and add columns to the left. I tried using the insert () function, and where my problem is.

Now my code is as follows:

 void Board::addRowTop() { Tile* t; int i = 0; maxY++; for ( ; i < maxX ; i++ ) playGround[i].insert(0, t); } 

Ignore the variables maxX and maxY; they are not relevant to this issue. The problem is this: insert (0, t) obviously does not work, since "t" is not the data type expected by the function.

My question is simple: why do I need a second insert () argument to solve this problem? I looked on google, but I could not find the correct answer.

Thank you very much in advance.

+4
source share
1 answer

I don’t think this is the argument β€œt” calling the function: playGround [i] is std::vector<Tile *> , so you should be able to insert Tile * s (although PLEASE say that you initialize 't' first - at least to NULL or 0!).

The problem is that the first argument to insert must be an iterator. Try:

 playGround[i].insert(playGround[i].begin(), t); 

Matt

+3
source

All Articles