Definition of C++ 2D Vector
In C++, Vectors are called dynamic arrays that have the capability to automatically resize itself when an item is inserted or removed, with its storage being controlled automatically by the container. 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 as well as accessed with the help of iterators. That is, the vector of a 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. But this is versatile compared to the matrix as elements can be added or deleted based on our requirements. Applications of 2-D vector include:
- Image representation and manipulation
- Representation of 2-D grid
- Applications in dynamic programming
Examples
Below are some sample programs on 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, first, a header file is mentioned for supporting the vector. Then, the elements of the vector are also defined while initializing the vector. Then, a loop is used to print 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:
In this program also, first, a header file is mentioned for supporting the vector. In addition to that, a constant R and C are defined for mentioning the number of rows and columns. Then, the elements of the vector are pushed back to the first vector using push_back() method. The starting value of the vector is mentioned as variable el. On executing the code, vector gets printed.
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:
In this program, similar to the above program elements are added. But, the last elements of the vector are removed using the pop_back() method, and the vector is printed before and after removing the elements for better understanding.
Conclusion
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 few examples respectively. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses