How to Create 2D Vectors in C++

In this article, we are going to learn how to create 2D vectors in C++ or you can say how to declare a 2-D Vector in C++ STL. Like we have 2D arrays in C++ same way we can use vectors for 2D data. We can save the data in the form of 2D vectors.

How to Declare a vector of vectors in C++


First of all, we will learn how to declare a 2-D vector or create a vector of vectors. The syntax to do this is very simple and also similar to how we create a normal vector. let us have a loop at the Syntax:

vector<vector<data_type>> vec;

Using this syntax we are creating many types of vectors in the below example. We are creating vectors of integers and vectors of strings.


Also as you can see in the example, we are creating it with vectors of different sizes. So vector of vectors does not need to have all same size vectors. It can be of any size and can grow or shrink at run time to give you freedom and flexibility.

  • Program to Declare a vector of vectors in C++.
  • Program to Declare a vector of vectors strings in C++.
  • Program to Declare 2-D vectors in C++.

Program to Declare a vector of vectors in C++

#include <iostream>
#include <vector>
using namespace std;
  

int main()
{

vector<vector<int>> vecint;

vector<vector<string>> vecstr;

vector<vector<int>> vec{ { 1, 2, 3 }, 
                         { 4, 5, 6 }, 
                         { 7, 8, 9 } }; 

vector<vector<int>> vec1{ { 1, 2, 3 }, 
                         { 4, 5, 6,7 }, 
                         { 8, 9, 10, 11 } }; 
						 
vector<vector<string>> vec2{ { "abc", "def", "ghi" }, 
                         { "jkl", "mno", "pqr" }, 
                         { "stu", "vwx", "yzz" } }; 

vector<vector<string>> vec3{ { "abc", "def", "ghi" }, 
                         { "jkl", "mno", "pqr","ppp" }, 
                         { "stu", "vwx", "yzz","yyy" , "zzz" } };
						 
						 
    cout <<"First vector elements are :"<< endl;
    for (int i = 0; i < 3; i++) {
        for ( auto it = vec[i].begin(); it != vec[i].end(); it++)
            cout << *it << " ";
        cout << endl;
    }
	
	cout <<"Second vector elements are :"<< endl;
    for (int i = 0; i < 3; i++) {
        for (auto it = vec1[i].begin();it != vec1[i].end(); it++)
            cout << *it << " ";
        cout << endl;
    }
	
	 cout <<"Third vector elements are :" << endl;
    for (int i = 0; i < 3; i++) {
        for (auto it = vec2[i].begin();it != vec2[i].end(); it++)
            cout << *it << " ";
        cout << endl;
    }
	
	 cout <<"Fourth vector elements are :" << endl;  
    for (int i = 0; i < 3; i++) {
        for (auto it = vec3[i].begin();it != vec3[i].end(); it++)
            cout << *it << " ";
        cout << endl;
    }

return 0;
}

Output

First vector elements are :
1 2 3 
4 5 6 
7 8 9 
Second vector elements are :
1 2 3 
4 5 6 7 
8 9 10 11 
Third vector elements are :
abc def ghi 
jkl mno pqr 
stu vwx yzz 
Fourth vector elements are :
abc def ghi 
jkl mno pqr ppp 
stu vwx yzz yyy zzz