test.size(); // size of the outer vector
// size of the inner vector at position 0
test[0].size(); // or
test.at(0).size(); // or
(*i).size(); // where i is an iterator to test[0]
First when you declare vectors of vectors do it like so vector< vector<int> > // Hint: Notice the space between the >'s
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <vector>
usingnamespace std;
int main()
{
vector< vector<int> > test {{2,3,4,5}, {7,8,9}};
// Prints the first elements size
// Which means it prints the size of the vector inside the fist element
cout << test[0].size();
return 0;
}
You have to remember that each element of the first vector is a vector itself and has all the functionality of a normal vector. So you just need to tell the program which element you want to access the size of and then use the size command.
I'm in the midst of the Brain Trust! Thanks people for all the answers =).
@Catfish3 - I'm learning from a C++11 updated book so yeah I'm up with the times! Unless that's sarcasm, then =(. Thanks for the code example. Yours and vlad's helped me figure out how to set up iterators instead. And Peter87 nudged on that.
@Zereo - Yeah the book warned me about the spacing, but it refers to older compilers. Appreciate the explanation, hits the nail in the coffin.
Is this a stylistic suggestion? Because in C++11 it's not an error to not use spaces here.
*it is not an error to exclude the space.
Why shall it be done as you showed?
Pre C++11 it is a error though so might as well cover your corners. It is a error because Pre C++11 the compiler mistook the two >>'s to mean the >> operator.
Also I guess it doesn't have to be done as shown but a lot of people don't have C++11 compilers and it would be in error for them.
That is true LB, so I guess the proper way of phrasing what I said should have been "Be warned though that if you are not using C++11 you need to declare it like so" instead of "you should declare it like so". My mistake for miss wording it.
@Zereo
Pre C++11 it is a error though so might as well cover your corners. It is a error because Pre C++11 the compiler mistook the two >>'s to mean the >> operator
It is not serious because using of '>" is not compatible with the previous standard also in some other cases. So there is no any sense to do what you suggested.
Ummm... I already admitted I probably phrased it wrong, but I don't see how telling someone that vector<vector<int>> can be in error for older compilers is giving bad advice.
Also have no idea what you are trying to say by
It is not serious because using of '>" is not compatible with the previous standard also in some other cases. So there is no any sense to do what you suggested.
@Zereo
Also have no idea what you are trying to say by
It is not serious because using of '>" is not compatible with the previous standard also in some other cases. So there is no any sense to do what you suggested.
I said very clear that there are other incompatibiles of using < and > in specifying template arguments between the previous and the current C++ Standards.
And sorry if I sounded rude I just don't really like to be told that I'm giving bad advice when in fact I did no such thing. Specially in that manner. Anyways just wanted to say sorry.