I need to resize a two dimensional vector (of unknown size) as follows:
//Creating the vector of vectors:
vector< vector< long > > v;
//Resizing the vectors:
/*Needing help here:
my current assumption is:
v.resize(1);
v[0].resize(1);
*/
//Adding elements:
v[0][0].push_back(0);
v[0][1].push_back(-1);
The compiler is reporting error upon applying the push_back. I think i have a problem in resizing the vector.
The objective is creating a two dimensional vector that allocates memory dynamically according to the added values.
Thanks in advance for your help
Your assumption about resizing the two dimensions is correct, but you are getting an error on push_back because the type of
v[0][0]islong &, notvector<long> &.I think you want:
Keep in mind though that push_back only resizes that particular row/column. If you’re not consistent about adding the same number of elements to
v[0],v[1], etc, you’ll end up with a “jagged” array. You may want to wrap this whole thing up in a class to enforce that consistency.