Pls explain vector of vector


#1

pls some send link or explain vectors of vectors
thank you.


#2

vector of vector i.e. "vector<vector > v " is same as 2-d array . The only difference is the representation and style of inserting the element or mostly the same.When we use 2-d array then while inserting the element we first insert in the first row and then in the other rows.Means we insert into array row-wise.Here is the same case.
if you want to insert into a 2-d array then you can do like this :
suppose you want to have a 2-d vector of size n x n .
declaration :
vector<vector > v;
I want to insert like the following :
1 1 1 1 1 …n times
2 2 2 2 2 …n times
. . . . .
. . . . .
n n n n n …n times
Then for this I will do this:
for(int i=1;i<=n;i++)
{
vector temp; // temporary vector
for(int j=1;j<=n;j++) temp.push_back(i); //pushing elements into temp vector column-wise
v.push_back(temp); // push the row vector one by one to make 2-d vector
temp.clear(): // make the vector size 0 by clearing all the elements.
}
so what i am doing here is that i am pushing 1-d vector into a another 2-dimensional vector .
Although everyone have been using different methods.It may be one of them.
Following are the link for vector :



You can search more ! Hope this helps !

#3

Let’s say you want to initialize 2D vector, m*n, with initial value to be 0

we could do this

#include<iostream>
int main(){ 
    int m = 2, n = 5;

    vector<vector<int>> vec(m, vector<int> (n, 0));

    return 0;
}

Then to print value you can use vec[i][j] as same as in array.