C++ 2D Vector with Examples – Vector of Vectors

It is a vector consisting of vectors. As the name suggests it is 2-Dimensional in nature and can help you solve problems. In this tutorial, you will learn how to work with 2d vectors in C++.

How to Take Input in 2D Vector?

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<vector<int>> allvect;
    int count;
    
    cout<<"Enter the number of vectors you have ";
    cin>>count;
    
    for(int i=0;i<count;i++) {
        int elements,num;
        
        cout<<"Enter the number of elements you have in this vector ";
        cin>>elements;
        
        vector<int> minivect; //Each vector is stored here
        
        for (int j=0;j<elements;j++) {
            cout<<"Enter the data of this item ";
            cin>>num;
            minivect.push_back(num);
            
            //push_back is used to insert an element in a vector
        }
        
        allvect.push_back(minivect);
        //Here each vector is pushed into the allvect
    }
    
    return 0;
}

Output:

Enter the number of vectors you have 2
Enter the number of elements you have in this vector 2
Enter the data of this item 2
Enter the data of this item 3
Enter the number of elements you have in this vector 2
Enter the data of this item 3
Enter the data of this item 4

Code Explanation:

First of all, we include all the important libraries including the vector library. Now we declare the 2D vector by vector<vector<int>> allvect. allvect is going to contain all the vectors as its elements.

Now we take the input that how many vector elements are there in the count variable.

After this, we run a for loop for taking input of all the elements of each vector. Then we use push_back function to add elements in allvect. 

Tip: You can use 2D vectors in all the cases of 2D array whereas vice versa is not possible.

How to Initialize 2D Vector?

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<vector<int>> allvect{{1,2,3},{4,5,6},{7,8},{9,10,11,12}};
    /*
    allvect contains 4 vectors having different sizes.
    Here are the vectors:
    1 2 3
    4 5 6
    7 8
    9 10 11 12
    */
    
    vector<vector<int>> vec(5);
    //vec is going to contain 5 vectors
    
    vector<vector<int>> newvector(5,vector<int> (6));
    //newvector is going to contain 5 vectors in which each vector is going to contain 6 elements
    
    return 0;
}

How to Print 2D Vector?

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<vector<int>> allvect{{1,2,3},{4,5,6},{7,8},{9,10,11,12}};
    
    int count=allvect.size();
    // size() function returns total number of elements in a vector.
    
    for(int i=0;i<count;i++) {
        vector<int> minivect=allvect[i];
        int elements=minivect.size();
        
        for(int j=0;j<elements;j++) {
            cout<<minivect[j]<<" ";
        }
        
        cout<<"\n";
    }
    
    return 0;
}

Output:

1 2 3
4 5 6
7 8
9 10 11 12

How to Iterate Through 2D Vector Using Iterators?

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<vector<int>> allvect{{1,2,3},{4,5,6},{7,8},{9,10,11,12}};
    
    for(auto minivect:allvect) {
        for(auto element:minivect){
            cout<<element<<" ";
        }
        
        cout<<"\n";
    }
    
    return 0;
}

How to Iterate Through 2D Vector Through Indexing?

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<vector<int>> allvect{{1,2,3},{4,5,6},{7,8},{9,10,11,12}};
    
    for(int i=0;i<allvect.size();i++) {
        for(int j=0;j<allvect[i].size();j++) {
            cout<<allvect[i][j]<<" ";
        }
        
        cout<<"\n";
    }
    
    return 0;
}

So these were some characteristics of a 2D vector in c++. Feel free to comment below if you have more doubts regarding it.

Leave a Comment

Your email address will not be published. Required fields are marked *