Updated July 1, 2023
Definition of C++ 2D Vector
In C++, Vectors are called dynamic arrays that can automatically resize themselves when an item is inserted or removed, with its storage being controlled automatically by the container. A 2-Dimensional Vector, also known as a vector of vectors, is a vector with an adjustable number of rows where each of the rows is a vector. Here, each vector index stores a vector that can be traversed and accessed with the help of iterators. That is, a vector’s vector is almost similar to a vector array, but the difference is only in the dynamic properties. In this article, we will discuss C++ 2D Vector in detail.
Syntax:
While learning a new concept in a programming language, you have to understand the basic syntax of the same. So, let us see the syntax of the two-dimensional vector.
vector<vector<data_type>> v;
How 2D Vector works in C++?
Now, we know the syntax of a two-dimensional vector. It is time to see a sample of the same.
vector<vector<int> > vtr{{34,55,43,13},{45,61,15,89},{53,62,17,12}
Here, a vector vtr is initialized with three rows and four columns. If we print this using a for loop, it will be displayed in the form of a vector.
From the vector appearance itself, we can know that it is similar to matrices. Applications of the 2-D vector include:
- Image representation and manipulation
- Representation of 2-D grid
- Applications in dynamic programming
Examples
Below are some sample programs on the 2-D vector.
Example #1
CPP program that Initializes a Two-Dimensional Vector.
Code:
#include <iostream>
//header file that is used for two dimensional vector
#include <vector>
using namespace std;
//main method
int main()
{
// initialize the two dimensional vector
vector<vector<int> > vtr {{34,55,43,13},{45,61,15,89},{53,62,17,12}};
//print the two dimensional vector initialised
cout<<"The two dimensional vector created is :"<<endl;
//loop to print the two dimensional vector
for ( int i = 0 ; i < vtr.size(); i++ )
{
//j loop
for ( int j = 0; j < vtr[i].size() ; j++ )
//print elements of vector
cout << vtr[i][j] << " " ;
cout << endl ;
}
return 0;
}
Output:
In this program, a header file is first mentioned for supporting the vector.
Example #2
CPP program that Initializes a Two-Dimensional Vector by Pushing a One-Dimensional Vector to the Back.
Code:
#include <iostream>
#include <vector>
#define R 3
#define C 4
using namespace std;
//main method
int main()
{
// initialize the two dimensional vector
vector<vector<int> > vtr ;
// items to be inserted in the vector
int el = 10;
// code for insertion of elements
for (int i = 0; i < R ; i++) {
// Vector that is used to store items of column
vector<int> vtr1;
for (int j = 0; j < C ; j++)
{
//value added to vector
vtr1.push_back(el);
el += 3;
}
// Push created vector for creating the 2 dimensional vector
vtr.push_back(vtr1);
}
//print the two dimensional vector initialised
cout<<"The two dimensional vector created is :"<<endl;
//loop to print the two dimensional vector
for ( int i = 0 ; i < vtr.size(); i++ )
{
//j loop
for ( int j = 0; j < vtr[i].size() ; j++ )
//print elements of vector
cout << vtr[i][j] << " " ;
cout << endl ;
}
return 0;
}
Output:
Example #3
CPP program that Initializes a Two-Dimensional Vector by pushing a One-Dimensional Vector to the back and Removing the Elements Later.
Code:
#include <iostream>
#include <vector>
#define R 3
#define C 4
using namespace std;
//main method
int main()
{
// initialize the two dimensional vector
vector<vector<int> > vtr ;
// items to be inserted in the vector
int el = 10;
// code for insertion of elements
for (int i = 0; i < R ; i++) {
// Vector that is used to store items of column
vector<int> vtr1;
for (int j = 0; j < C ; j++)
{
//value added to vector
vtr1.push_back(el);
el += 3;
}
// Push created vector for creating the 2 dimensional vector
vtr.push_back(vtr1);
}
//print the two dimensional vector initialised
cout<<"The two dimensional vector created is :"<<endl;
//loop to print the two dimensional vector
for ( int i = 0 ; i < vtr.size(); i++ )
{
//j loop
for ( int j = 0; j < vtr[i].size() ; j++ )
//print elements of vector
cout << vtr[i][j] << " " ;
cout << endl ;
}
// Remove last items from the created vector
vtr[2].pop_back();
vtr[1].pop_back();
//print the two-dimensional vector after removing elements
cout<<"The two dimensional vector after removing elements is :"<<endl;
//loop to print the two dimensional vector
for ( int i = 0 ; i < vtr.size(); i++ )
{
//j loop
for ( int j = 0; j < vtr[i].size() ; j++ )
//print elements of vector
cout << vtr[i][j] << " " ;
cout << endl ;
}
return 0;
}
Output:
Conclusion
A 2-Dimensional Vector is a vector with an adjustable number of rows where each of the rows is a vector. In this article, different aspects of the 2-D vector are explained in detail.
Recommended Articles
This is a guide to C++ 2D Vector. Here we discuss the definition and How 2D Vector works in C++? along with a few examples respectively. You may also have a look at the following articles to learn more –