Multidimensional Vectors in C++
Last Updated :
18 Nov, 2024
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:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// A 2D vector
vector<vector<int>> v = {{1, 2, 3}, {4, 5, 6},
{7, 8, 9}};
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v[i].size(); j++)
cout << v[i][j] << " ";
cout << endl;
}
return 0;
}
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:
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.
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:
C++
#include <bits/stdc++.h>
using namespace std;
void printV(vector<vector<int>>& v) {
for (auto i: v) {
for (auto j: i) {
cout << j << " ";
}
cout << endl;
}
cout << endl;
}
int main() {
// An empty 2D vector
vector<vector<int>> v1;
// 2D vector with initial size and value
vector<vector<int>> v2(2, vector<int>(3, 11));
// A 2D vector initialized with initializer list
vector<vector<int>> v3 = {
{1, 2, 3},
{4, 5, 6},
};
printV(v1);
printV(v2);
printV(v3);
return 0;
}
Output11 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.
Operation | Description |
---|
Access Elements | To access an element, first access the vector at the i-th index, then access the j-th element of this vector. |
Update Elements | Assign the accessed element a new value using the assignment operator. |
Insert Elements | The 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 Elements | To 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 Vector | Traversal 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
C++
#include <iostream>
#include <vector>
using namespace std;
void printV(vector<vector<int>>& v) {
// Traversing the 2D vector
for (auto& i : v) {
for (auto& j : i) {
cout << j << " ";
}
cout << endl;
}
cout << endl;
}
int main() {
vector<vector<int>> v;
// Insert whole vectors
v.push_back({1, 2, 3});
v.push_back({4, 6});
// Insert in some specific row
v[1].insert(v[1].begin() + 1, 5);
printV(v);
// Update the element at index 2 in first row
v[0][2] = 10;
printV(v);
// Remove the element at index 1 of the second row
v[1].erase(v[1].begin() + 1);
printV(v);
printV(v);
return 0;
}
Output1 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.
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.
C++
#include <bits/stdc++.h>
using namespace std;
void printV(vector<vector<vector<int>>> &v) {
for (auto i : v) {
for (auto j : i) {
for (auto k : j)
cout << k << " ";
cout << endl;
}
cout << endl;
}
}
int main() {
// An empty 3D vector
vector<vector<vector<int>>> v1;
// 3D vector with initial size and value
vector<vector<vector<int>>> v2(2, vector<vector<int>>
(2, vector<int>(3, 9)));
// 3D vector initialized with initializer list
vector<vector<vector<int>>> v3 = {{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}}};
printV(v1);
printV(v2);
printV(v3);
return 0;
}
Output9 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.
Operation | Description |
---|
Access Elements | To 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 Elements | Assign the accessed element a new value using the assignment operator. |
Insert Elements | To 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 Elements | To 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 Vector | Traversal 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.
C++
#include <bits/stdc++.h>
using namespace std;
// Function to Traverse 3D vector
void printV(vector<vector<vector<int>>>& v) {
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v[i].size(); j++) {
for (int k = 0; k < v[i][j].size(); k++)
cout << v[i][j][k] << " ";
cout << endl;
}
cout << endl;
}
for (int i = 0; i < 15; i++) {
cout << '-';
}
cout << endl << endl;
}
int main() {
vector<vector<vector<int>>> v;
// Insert 2D vectors
v.push_back({{1, 2, 3}, {4, 5, 6}});
v.push_back({{7, 8, 9}, {10, 11, 12}});
printV(v);
// Access and Update element at (0, 1, 2)
v[0][1][2] = 99;
cout << v[0][1][2] << "\n\n";
// Delete element at (1, 1, 1)
v[1][1].erase(v[1][1].begin() + 1);
printV(v);
return 0;
}
Output1 2 3
4 5 6
7 8 9
10 11 12
---------------
99
1 2 3
4 5 99
7 8 9
10 12
---------------
Examples of Multidimensional Vectors
The below examples demonstrate some of the common operations and examples of multidimensional vectors in C++:
Find Column and Row Size of a 2D Vector in C++
How to Resize a 2D Vector in C++?
Sorting 2D Vector in C++ | Set 1 (By row and column)
How to flatten a Vector of Vectors or 2D Vector in C++
Similar Reads
List of Vectors in C++ STL
In C++, the list of vector refers to the list container in which each element is a vector. In this article, we will learn about the list of vectors in C++.Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { list<vector<int>> l = {{1, 3}, {2
3 min read
C++ Multidimensional Array
A multidimensional array is an array with more than one dimension. It means that it can grow in different directions i.e. instead of changing the length only, it can also change in width, depth or more.Syntax of Multidimensional ArraysC++data_type array_name[s1][s2]...[sn];where s1, s2,â¦, sn are the
8 min read
Multidimensional Pointer Arithmetic in C/C++
In C/C++, arrays and pointers have similar semantics, except on type information. As an example, given a 3D array int buffer[5][7][6]; An element at location [2][1][2] can be accessed as "buffer[2][1][2]" or *( *( *(buffer + 2) + 1) + 2). Observe the following declaration T *p; // p is a pointer to
4 min read
Vector size() in C++ STL
In C++, the vector size() is a built-in method used to find the size of a vector. The size of a vector tells us the number of elements currently present in the vector. In this article, we will learn about the vector size() method.Let's take a look at the simple code example:C++#include <bits/stdc
3 min read
multimap::operator= in C++ STL
multimap::operator= is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new contents. Syntax:- multimap1 = (multimap2) Parameters : Another container of the same type. Result : Assign the contents of the container passed as p
2 min read
multimap operator = in C++ STL
The multimap::operator= is a built in C++ STL which assigns new contents to the container, replacing its current content. Syntax: multimap1_name = multimap2_name Parameters: The multimap on the left is the container in which the multimap on the right is to be assigned by destroying the elements of m
2 min read
Vector in C++ STL
C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
Vector max_size() in C++ STL
In C++, vector max_size() is a built-in function used to find the maximum number of elements that can be held by the vector container. In this article, we will learn about the vector max_size() function in C++.Let's take a look at an example that illustrates this method:CPP#include <bits/stdc++.h
3 min read
Vector of Strings in C++
In C++, a vector of strings is a std::vector container that stores multiple strings. It is useful when you need to store a collection of string data in a single container and refer to them quickly. In this article, we will learn about the vector of strings and how to create and use it in C++.Table o
3 min read
Implementing Multidimensional Map in C++
Multidimensional maps are used when we want to map a value to a combination of keys. The key can be of any data type, including those that are user-defined. Multidimensional maps are nested maps; that is, they map a key to another map, which itself stores combinations of key values with correspondin
3 min read