## Syntax:

The syntax of the two-dimensional vector has given beneath.

vector<vector<data_type>> vector_identify

A specific data variety is described at the time of vector declaration. If the vector dimension is not defined then the vector is called an empty vector. The dimensions of the vector can be modified by working with distinct solutions or initializing the vector.

## Case in point-1: Produce a 2-Dimensional vector of an equivalent number of columns

The adhering to case in point exhibits the way of declaring a 2-dimensional vector of 3 rows and four columns that contains character facts. In this article, the values of the vector have been described at the time of vector declaration and the nested ‘**for**‘ loop has been employed to print the values of the vector.

#contain

#include things like

working with namespace std

int major()

/*

Declare a two-dimensional vector

of figures

*/

vector<vector>chrVector

‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’

//Print the values of the vector

cout<< “The values of the vector are:n“

for (int i = 0 i<chrVector.size() i++)

for (int j = 0 j <chrVector[i].size() j++)

cout<<chrVector[i][j] << ” “

cout<< ‘n‘

return 0

**Output:**

The following output will appear after executing the above code.

## Example-2: Create a 2-Dimensional vector with a different number of columns

The following example shows the way of declaring a 2-dimensional vector of four rows where the first row contains one column, the second row contains two columns, the third row contains three columns, and the fourth row contains four columns. The vector has initialized with the integer data and printed using the nested ‘**for**’ loop.

#include

#include things like

employing namespace std

int key()

/*

Initialize the 2D vector with the

integer selection exactly where just about every row incorporates diverse

variety of factors

*/

vector<vector>intVector

20, 10, 30, 50, 40, 60 , 80, 10, 70, 90

//Print the values of the vector applying for loop

cout<< “The values of the vector are:n“

for (vectorrow :intVector)

for (int val : row)

cout<<val<< ” “

cout<< ‘n‘

return 0

**Output:**

The following output will appear after executing the above code.

## Example-3: Initialize a 2-Dimensional empty vector with the default value

The way to declare a 2-dimensional empty vector of float number and initialize the vector with a float number has shown in the following example. Here, the nested ‘for’ loop has been used to insert data into the vector using **the push_back() **function and print the values of the vector.

The default value, 6.5 has been inserted into the vector by creating 2 rows and 3 columns. The **size() **function has been used to count the total rows and columns for printing the values of the vector.

#include

#involve

applying namespace std

int key()

//Established the default value

float default_worth = 6.5

//Outline the outer vector

vector<vector>outVect

for (int i = i< 2 i++)

//Define the inner vector

vectorinVect

for (int j = 0 j < 3 j++)

//Insert the default value

inVect.push_back(default_value)

//Insert the inner vector to outer vector

outVect.push_back(inVect)

//Print the values of the vector

cout<< “The values of the vector are:n“

for (int i = 0 i<outVect.size() i++)

for (int j = 0 j <outVect[i].size() j++)

cout<<outVect[i][j] << ” “

cout<< ‘n‘

return 0

**Output:**

The following output will appear after executing the above code. The output shows the content of the vector based on the default value and the number of rows and columns created by the code.

## Example-4: Initialize a 2-Dimensional empty vector by taking input values

The way to create a 2-dimensional vector by taking input from the user has shown in the following example. A 2-Dimensional empty vector of an integer has been declared in the code that will contain 2 rows and 3 columns.

The nested ‘**for**‘ loop has been used to take 6(2×3) integer numbers from the user and insert them into the vector using index values. Another nested ‘**for**‘ loop has been used to print the inserted values of the vector.

#include

#include things like

working with namespace std

int principal()

//Determine the variety of cols

int col = 3

//Define the amount of rows

int row = 2

//Initialize an integer variable

int val =

//Initialize the vacant vector

vector< vector>int2DVector

//Resize the outer vector

int2DVector.resize(row)

for (int i = i< row i++)

//Resize the inner vector

int2DVector[i].resize(col)

for (int j = 0 j < col j++)

//Take input from the user

cout<>val

//Insert into the vector

int2DVector[i][j] = val

//Print the values of the vector

cout<< “The values of the vector are:n“

for (int i = 0 i< int2DVector.size() i++)

for (int j = 0 j < int2DVector[i].size() j++)

cout<< int2DVector[i][j] << ” “

cout<< ‘n‘

return 0

**Output:**

The following output will appear after executing the above code. The output shows the 6 input values and the content of the vector based on the number of rows and columns.

## Conclusion

A 2-Dimensional vector is used in C++ programming to store and access data based on rows and columns. Different ways to create a 2-Dimensional vector have been shown in this tutorial by using simple examples. The purpose of using the 2-Dimensional vector in C++ will be cleared after reading this tutorial.