-
Vector of Vectors
Hello;
I need to know how to declare a vector of vectors and how to access.
vector <int> int_vec;
#need a vector containing vectors.
Then I need to do
int_vec.push_back(integer);
int_vec.push_back(integer2);
vector_Containing vectors.push_back(int_vec);
clear int_vec for use again
Also, how do I reference the integer now
ie The integer in container vector position 0 and integer vector position 1.
Thanks
-
-
You know the syntax for declaring a vector of int, a vector of vectors of int is only a small step.
The syntax for declaring a vector is
Code:
vector<data_type> vector_name;
So, in your case, you want a vector of vectors of int. So put vector<int> in for data_type, and we get
Code:
vector<vector<int> > vector_name;
In general, the rule on whitespace in your code is that it's ignored, that is:
Code:
cout << someVar;
cout<<someVar;
cout<< someVar;
cout
<<
someVar;
are all the same. In the above declaration, the space between the two greater-than signs is neccessary. The compiler will be confused thinking you're using the insertion operator. That doesn't sound so clear re-reading it, so as an example:
Code:
vector<vector<int> > vecName; //this is good
vector<vector<int>> vecName; //BAD swanley! NAUGHTY! {swats you on the nose with a newspaper}
As far as pushing elements onto the vectors and accessing elements in a vector of vectors, do some more research. Try to see if you can get it on your own, and if not, post what you've tried and why you think it's not working.