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);
}
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 )
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?
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.
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.
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 ...