Vector Vector C ++

advertisements

Vector Vector C ++ Hi, I do not understand the syntax of nested vectors to simulate an array, I have the following code.

vector< vector< float> > myvector (n, vector < float> (2));

But I do not quite understand how it works, especially where you specify the size of the vectors and the vectors in it, if you want to make a resize so that my vector vector has specified dimensions how can I resize internal vectors? Something like changing vec [10] [2] to vec [10] [5] (changing the second dimension)

In addition to how to make copies with multidimensional vectors something like:

vector< int> myvector (myVectorToCopy, myVectorToCopy+myVectorToCopy.size());

But with several dimensions. Thanks.


vector<vector<float>> means you are creating a vector which contains a vector of floats. The constructor argument means you are creating a vector of size n where each element of vector is a vector of floats having size 2.

To resize the vector<vector<float>>:

for (int i = 0; i < n; ++i)
    A[i].resize(newSize);

Alternatively you could use:

A.assign(n,vector<float>(newSize));

To make a copy of multidimensional vector use constructor:

vector<vector<float>> B(A);