# multi dimensional vectors

• 03-17-2005
r0bbb
multi dimensional vectors
Hi i was wondering if it was possible to set a specific value in a vector. ill explain what i mean in arrays, if you could please show me the vector equivelent.

first i declare my multidimensional array / vector

Tile tiles[50][50]; OR vector<vector<Tile> > tiles;

next i have a loop to loop thru each element and initialise it.

array version:
Code:

```          for(int j = 0; j < mapHeight; j++){               for(int i = 0; i < mapWidth; i++){                   tiles[i][j].init();               }           }```
how would i perform this action on a multidimensional vector. baring in mind that i need to set that specific x/y value.

i tried the above with vectors but it doesnt seam to work.

any help will be great as i cant find much on vectors containing objects on the internet.

rob
• 03-17-2005
Daved
The for loop is the exact same assuming mapHeight and mapWidth are correct.

The problem might be in your constructor. Tile tiles[50][50]; is NOT the same as vector<vector<Tile> > tiles; because the vector constructor you are using creates an empty vector. If you aren't using push_back to add inner vectors, you are probably not getting the results you want.

There are many ways to solve this, but here is the equivalent of a 50 x 50 2-D vector of default constructed Tiles:
Code:

`vector<vector<Tile> > tiles(50, vector<Tile>(50));`
• 03-17-2005
IfYouSaySo
I have given an example to do what you want. In my comments below, I repeatedly ask, "Why not just call init() inside the Tile constructor?" That really is the best solution, but I show how to iterate over each element anyway using standard algorithms.

You might also consider writing a data structure to hold your vector< vector<Tile> >, so that you can make add, remove, etc. easier.

Code:

```# include <vector> # include <iostream> # include <algorithm> using namespace std; class Tile {     public:     Tile() {         // Why not call init here??         init();     }     void init()     {         cout << "Init called!" << endl;     } }; void init_tile(Tile& t) {     /* Why not do it in the Tile constructor? */     t.init(); } void init_row(vector<Tile>& row) {     for_each(row.begin(), row.end(), init_tile); } int main() {     vector< vector<Tile> > tiles;     /* Add some rows */     tiles.push_back(vector<Tile>());     tiles.push_back(vector<Tile>());     tiles.push_back(vector<Tile>());     /* Add some tiles */     tiles.at(0).push_back(Tile());     tiles.at(1).push_back(Tile());     tiles.at(0).push_back(Tile());     tiles.at(2).push_back(Tile());     /* Initialize here, if for some reason you can't just         do it in the constructor... */     for_each(tiles.begin(), tiles.end(), init_row); }```
• 03-17-2005
r0bbb
daveds method worked, thanks a lot! the reason i dont call init in the constructor is because i want to check whats returned.

cheers for all ur help

rob
• 03-18-2005
VirtualAce
Code:

```for(int j = 0; j < mapHeight; j++){               for(int i = 0; i < mapWidth; i++){                   tiles[i][j].init();               }           }```
Code:

```unsigned long maxoffset=(mapHeight*mapWidth); for (int i=0;i<maxoffset;i++) {   Tiles[i].init(); }```

My advice. Don't use 2D vectors.