Resizing a Vector of Vectors

I'm trying to figure out how to resize a vector of vectors based upon the user's input. I'm making a method called setGrid which should take in the # of rows and columns and then generate a chessboard based upon that. I can resize outer vector no problem but how would I resize the inner vectors too? This is what I have right now so you can see where my head is at.

1
2
3
4
5
6
7
8

grid_ = vector<vector<int>>(9,vector<int>(9));

  void setGrid (int new_i, int new_j)
{
	//trying to figure out how to resize the grid based on input from user
	grid_.resize(new_i);
}
First of all aren't chess boards 8 x 8 not 9 x 9?
Secondly you have a nono on line 2 it is supposed to be > > not >>.
secondly try something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <vector>
void resizeVec( std::vector<std::vector<int> > &vec , const unsigned short ROWS , const unsigned short COLUMNS )
{
    vec.resize( ROWS );
    for( auto &it : vec )
    {
        it.resize( COLUMNS );
    }
}
int main()
{
     const auto INITIAL = 9;
     auto rows = INITIAL , columns = INITIAL , count = 0;
     std::vector<std::vector<int> > vec( INITIAL , std::vector<int>(INITIAL) );
     std::cout << "How many rows?\n> " << std::flush;
     std::cin >> rows;
     std::cout << "How many columns>\n> " << std::flush;
     std::cin >> columns;
     resizeVec( vec , rows , columns );
     std::cout << "Vec has " << vec.size() << " row." << std::endl;
     for( const auto &it : vec )
     {
         std::cout << "Row " << count << " has " << it.size() << " columns." << std::endl;
         ++count;
     }
}
How many rows?
> 4
How many columns>
> 2
Vec has 4 row.
Row 0 has 2 columns.
Row 1 has 2 columns.
Row 2 has 2 columns.
Row 3 has 2 columns.

Process returned 0 (0x0)   execution time : 1.981 s
Press any key to continue.
Sorry, it was just initialized randomly as 9x9 so I could test it and make sure it was constructed correctly. Your code makes sense and is incredibly helpful and I can see where you're going but what is "auto &it : vec"? When I try putting that in my code to try and wrap my head around it, I get errors saying "it" needs to be initialized. What exactly is "it", and what does the "auto" function do?
Thanks
auto is a variable type that gets the variable type automatically and it is just a variable name like i but I chose it because it is short for iterator the & is for the reference because I am iterating based on the reference not the value

There are other ways you can iterate but I did it the c++11 method by doing
iterator : thingToBeIterated

Another method ( not tested but should be similar )
 
for( std::vector<std::vector<int> >::iterator it = vec.begin(); it != vec.end(); ++it )
for(const auto &it : vec) is a ranged based loop that is like:
1
2
for(const auto it = vec.begin(); it != vec.end(); it++)
   //auto is a type deducer 

This is only available with C++11, so check your settings.

@gilbit
Spacing does not matter on line 2.
vector<vector<int>> is exactly the same as vector< vector<int> >
Just as
 
bool var = ((1+(1+2)) == (1+ (1+2) ));
Oh well looks like the inserter operator
If i'm using microsoft visual C++ 2010 Express, does that not work for me? If so how would I get around that? Because as of right now this code:
1
2
3
4
for(const auto it = vec.begin(); it != vec.end(); it+1)    
{
        it.resize( COLUMNS );
 }


gives an error with the .resize function because it says it is not of a vector type and it can't call the .resize function
Last edited on
Because it is a vector of vectors not a vector the method I used worked as long as the compiler supports c++11
I see why yours works but i'm worried my compiler (microsoft visual C++ 2010 Express) does not support c++11 because it is giving me an error. Any ways around this?

Thanks for being patient and so helpful.
Last edited on
Go to the toolbar on the top and there should be either a compiler tab or a settings tab with a compiler option under that. Once there either check the flag that says c++11 or under the other settings put this
-std=c++11 if it is GCC as a compiler that is.
It should be
it->resize( COLUMNS )
Also
it+1
should be
++it

Simpler:

1
2
3
4
5
6
7
8
void resizeVec( std::vector<std::vector<int> > &vec , const unsigned short ROWS , const unsigned short COLUMNS )
{
    vec.resize( ROWS );
    for( std::vector<std::vector<int> >::iterator it = vec.begin(); it != vec.end(); ++it)
    {
        it->resize( COLUMNS );
    }
}

EDIT: Typos fixed.
Last edited on
That worked perfectly! Thank you so much!
@Daleth

Spacing does not matter on line 2.

Remember that C++03, unlike C++11, doesn't like >> in nested template declarations; you need a space between the >s

// C++03 and C++11 :-)
vector<vector<int> >(9,vector<int>(9));

// C++03 :-( C++11 :-)
vector<vector<int>>(9,vector<int>(9));

Not everyone has switched to C++11 yet.

Andy
Last edited on
Ah, okay. Good to know.
In fact he didn't switch to C++11, as auto wasn't a keyword/type for him, and for(x:y) didn't work, too.
MSVC++ allows for those template declarations even for C++03, but puts in a warning.
Last edited on
MSVC++ allows for those template declarations even for C++03, ...

Unless you're unlucky enough to work on a legacy codebase which is still using a pre-2005 version of Visual Studio... :-(

Andy

Versions of Visual Studio prior to Visual Studio 2005 required that whitespace be inserted between template parameter lists when nested template instances were declared. The following syntax is now allowed in Visual Studio 2005 ...

Template Specifications
http://msdn.microsoft.com/en-us/library/x5w1yety%28v=vs.80%29.aspx

Last edited on
Oh, well, I meant VS10... Good to point things out anyways.
Topic archived. No new replies allowed.