Introduction

Dynamic arrays, or vectors in C++, can dynamically enlarge themselves when an item is added or withdrawn, with the container controlling its storage on an automated basis. A two-dimensional vector, sometimes referred to as a vector of vectors, is a vector with a variable number of rows, each of which is a vector. Each vector index in this case contains a vector that iterators may both traverse and access. In other words, the only difference between a vector of a vector and a vector array is in the dynamic features. We shall go into great detail about C++ 2D Vector in this blog.

Syntax

You must comprehend the fundamental grammar of a programming language while learning a new notion in that language. So let's examine the two-dimensional vector's syntax.

vector<vector<data_type>> v;

How does a 2D Vector work in C++?

We now understand how a two-dimensional vector is written. Time to see an example of the same, then.

vector<vector<int> > vtr{{34,55,43,13},{45,61,15,89},{53,62,17,12}

Three rows and four columns are used to initialize the vector vtr in this instance. This will be presented as a vector if we print it using a for loop. We can tell a vector looks like a matrix just by looking at it. However, this is more flexible than the matrix since elements may be added or removed based on our needs. Among the uses for a 2-D vector are:

  • Representation and modification of images
  • Applications of the 2-D grid
  • For representation in dynamic programming

Examples

Here are a few 2-D vector example applications.

Example #1 - Initialization of a two-dimensional vector using CPP.

Code:

#include <iostream>
#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 initialized
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;
}

A header file for handling the vector is first described in this application. When initializing the vector, the members of the vector are also specified. The vector is then printed using a loop.

Example #2 - Using a one-dimensional vector pushed to the back, the CPP code initializes a two-dimensional vector.

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 initialized
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;
}

write your code here: Coding Playground

In this code, too, a header file for supporting the vector is specified initially. Additionally, constants R and C are provided to specify the number of rows and columns, respectively. The vector's items are then returned to the first vector by utilizing the pushback () function. Variable el is used to refer to the vector's initial value. A vector is printed once the code has been executed.

Conclusion

A vector with two dimensions has a customizable number of rows, and each row contains a vector. The 2-D vector's many characteristics are thoroughly covered in this article.
Hope this article gave you a clear understanding of the concept. For similar topics visit Board Infinity Blogs and Discussion Forum.