# resizing vectors

• 02-10-2006
agilman
resizing vectors
Howdy,

I have two ints:
int sizeX;
int sizeY;

I assign them values...
and then I want to make a multidimensional vector that size.

vector<vector<char > > maze;
so that maze will be sizeX*sizeY

• 02-11-2006
Dave_Sinkula
Quote:

Originally Posted by agilman

Why don't you post that code? Certainly it's not the old 1 to N thing.
• 02-11-2006
swoopy
>Im having trouble looping through and doing push_back() when its multidimensional
Method 1 (using resize):
Code:

```  vector<vector<char > > maze;   maze.resize(sizeX);   for (int i=0; i<sizeX; i++)   {       for (int j=0; j<sizeY; j++)       {         maze[i].push_back(some_char);       }   }```
Method 2:
Code:

```  vector<vector<char > > maze(sizeX, vector<char>(sizeY));   for (int i=0; i<sizeX; i++)   {       for (int j=0; j<sizeY; j++)       {         maze[i][j] = some_char;       }   }```
• 02-11-2006
agilman
Thanks swoopy!
Thats exactly what I was looking for!
• 02-11-2006
7stud
Quote:

Im having trouble looping through and doing push_back() when its multidimensional.
Code:

```int sizeX = 3; int sizeY = 2; vector<vector<int> > v; vector<int> temp; for(int i = 0; i<sizeX; i++) {         for(int j = 0; j<sizeY; j++)         {                 temp.push_back(i*j);         }         v.push_back(temp);         temp.clear(); } for(vector<vector<int> >::iterator iter = v.begin(); iter != v.end(); ++iter) {         for(vector<int>::iterator jter = (*iter).begin(); jter != (*iter).end(); ++jter)         {                 cout<<*jter<<" ";         }         cout<<endl; }```
Of course, you shouldn't restrict yourself to using push_back():
Code:

```int sizeX = 4; int sizeY = 3; vector<vector<int> > v(sizeX, vector<int>(sizeY)); for(int i = 0; i<sizeX; i++) {         for(int j = 0; j<sizeY; j++)         {                 v[i][j] = i*j;         } } for(i = 0; i<sizeX; i++) {         for(int j = 0; j<sizeY; j++)         {                 cout<<v[i][j]<<" ";         }         cout<<endl; }```