resizing vectors

This is a discussion on resizing vectors within the C++ Programming forums, part of the General Programming Boards category; Howdy, I have two ints: int sizeX; int sizeY; I assign them values... and then I want to make a ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    6

    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

    Im having trouble looping through and doing push_back() when its multidimensional... please help.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by agilman
    Im having trouble looping through and doing push_back() when its multidimensional... please help.
    Why don't you post that code? Certainly it's not the old 1 to N thing.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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;
          }
       }
    Last edited by swoopy; 02-11-2006 at 07:36 PM. Reason: Changed a vector<int> to vector<char>

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    6
    Thanks swoopy!
    Thats exactly what I was looking for!

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 01:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21