Vector of vector

How to input vector of vector when we just have the number of vectors and size of each vector. Elements are to be taken as input from user.

When the elements are already known it is simply like this-
vector<vector > a{ {12,3,4,1,5}, {2,5,6,24,7} ,{16,73,8,1} };

But when we just know the number of vectors to be made and size of each vector, then how can it be written?

Hi @Kishan-Mishra-2464674013761441,
you want to make a vector of vectors of something(lets say int) so first do that by:

vector < vector < int > > vec ;

Now you know vec will be containing vector of ints so you can do the following:

for i:0 -> n
    vector< int > v ;             // make a vector of just ints
     for j:0 -> m                   // lets fill out like a normal 1D array or vector
        int x ;
        cin >> x ;
        v.push_back(x) ;
    vec.push_back(v) ;

Since vec is a vector of vectors it will contain vecotrs and we can push_back inside it just like we pushed ints in case of a vector of ints.

Hope this helps :slight_smile:

1 Like

That was really helpful. Thanks a lot.