VOOZH about

URL: https://www.geeksforgeeks.org/cpp/multidimensional-vectors-in-cpp/

⇱ Multidimensional Vectors in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Multidimensional Vectors in C++

Last Updated : 23 Jul, 2025

A multidimensional vector is a vector of vectors that allows us to create 2D, 3D, or higher-dimensional vectors in a similar way as multidimensional arrays but unlike arrays, multidimensional vectors can grow and shrink dynamically.

Let's take a quick look at a simple example of a multidimensional array:


Output
1 2 3 
4 5 6 
7 8 9 

Explanation: We created a 2D vector in which each element is a vector in itself.

Types of Multidimensional Vectors

A multidimensional vector can be of any type depending on the dimensions, but the most commonly used multidimensional vectors are:

  • 2D Vectors
  • 3D Vectors

In this article, we will primarily discuss these two multidimensional vectors and how to use them in a C++ program.

2D Vectors

In C++, 2D Vectors or Two-Dimensional Vectors are the vectors in which each element is a 1D vector of any type. It is generally used to store the information in the tabular form where each vector element represents a row in the table and each element in this vector represents a column.

👁 2d-vectors-in-cpp

Syntax to Create a 2D Vector

The different syntax to create and initialize 2D vectors in C++ are as follows:

vector<vector<T>> v; // Empty 2D vector
vector<vector<T>> v(n, vector<T>(m, val)); // With n rows, m columns, val default value
vector<vector<T>> v = {{v1, v2,....}, // With given values
{x1, x2, ...,
........};

The below example demonstrates these methods of to create a vector:


Output
11 11 11 
11 11 11 

1 2 3 
4 5 6 

To know more methods of initialization, refer to this article - Initialize 3D Vector in C++

Basic Operations

The basic operations on 2D vectors have to be done by keeping in mind that each element is a vector.

OperationDescription
Access ElementsTo access an element, first access the vector at the i-th index, then access the j-th element of this vector.
Update ElementsAssign the accessed element a new value using the assignment operator.
Insert ElementsThe easiest way is to push the whole array using push_back(). But to insert an element at v[i][j], access the vector at the i-th index, and then use vector insert() to add the element at the j-th index.
Delete ElementsTo delete the element at v[i][j], access the vector at the i-th index, and then use erase() to remove the element at the j-th index.
Traverse VectorTraversal requires iterating through the 2D vector and each of its individual row (member vectors).

The below example demonstrates how to perform these basic operations on a 2D vector


Output
1 2 3 
4 5 6 

1 2 10 
4 5 6 

1 2 10 
4 6 

1 2 10 
4 6 

Note: Other vector insertion, deletion methods can also be used but remember to first access the appropriate row (member vector).

3D Vectors

In C++, 3D Vectors or Three-Dimensional Vectors are vectors where each element is a 2D vector. These vectors are used to represent data in a 3D space which can be visualized as the space where each 2D vector can be thought of as a layer, and each element within it represents rows and columns.

👁 3d-vectors-in-cpp

Syntax to Create a 3D Vector

The different syntax to create and initialize 3D vectors in C++ are as follows:

vector<vector<vector<T>>> v;
vector<vector<vector<T>>> v(x, vector<vector<T>>(y, vector<T>(z, val)));
vector<vector<vector<T>>> v = {{{...}, {...}}, {{...}, {...}}};

As we can see, the syntax of 3D vector is more complex than 2D vector and this complexity keep growing with additional dimensions. Let's look at a simple example to create a 3D vector.


Output
9 9 9 
9 9 9 

9 9 9 
9 9 9 

1 2 3 
4 5 6 

7 8 9 
10 11 12 

Basic Operations

The below table shows how to perform the basic operations such as insertion, deletion on 3D vectors:

The basic operations on 3D vectors must account for the fact that each element is a 2D vector.

OperationDescription
Access ElementsTo access an element, first access the 2D vector at the i-th index, then the row at the j-th index, and finally the k-th element in that row.
Update ElementsAssign the accessed element a new value using the assignment operator.
Insert ElementsTo insert an element at v[i][j][k], access the 2D vector at i-th index, then the row at j-th index, and use insert() to add an element at k-th index.
Delete ElementsTo delete the element at v[i][j][k], access the 2D vector at i-th index, the row at j-th index, and use erase() to remove the element at k-th index.
Traverse VectorTraversal requires iterating through the 3D vector, each 2D vector (layer), and its individual rows and columns.

Note: Other vector insertion and deletion methods can also be used, but ensure you first access the appropriate 2D vector (layer) or row.

The following example demonstrates how to create a 3D vector and perform basic operations like accessing, updating, inserting, deleting, and traversing elements.

Comment
Article Tags: