How to Initialize 3D Vector in C++ STL?
Last Updated :
19 Nov, 2024
Initializing 3D vector refers to the process of assigning the initial values to the elements of 3D Vector. In this article, we will learn different methods to initialize the 3D vector in C++.
The simplest ways to initialize the 3D vector is by passing the elements inside the initializer list. This method is convenient for quick initialization when the number of items is less.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// Initialize the 3D vector
vector<vector<vector<int>>> v = {{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}}};
for (auto i : v) {
for (auto j : i) {
for (auto k : j)
cout << k << " ";
cout << endl;
}
}
return 0;
}
Output1 2 3
4 5 6
7 8 9
10 11 12
Explanation: The elements specified in the initializer list are used as initial value of 3D vector.
Apart from the above method, C++ also provides other different methods to initialize a 3D vector depending on the situation. Some of them are:
By Default Value
The 3D vector can also be initialized with the default value at the time of declaration.
Syntax
vector<vector<vector<int>>> v(x, vector<vector<int>>(y, vector<int>(z, k)));
where x, y and z is the number of rows, number of column and depth of 3D vector respectively, and k is the default value by which we initialize the 3D vector.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// Initialize 3D vector with default value 11
vector<vector<vector<int>>> v(2,
vector<vector<int>>(2, vector<int>(3, 11)));
for (auto i : v) {
for (auto j : i) {
for (auto k : j)
cout << k << " ";
cout << endl;
}
}
return 0;
}
Output11 11 11
11 11 11
11 11 11
11 11 11
Note: In this method, if we do not initialize the 3D vector with some default value, then by default it will be initialized by value of the int type which is 0.
Using fill() Function
The fill() function can also be used to initialize the 3D vector with any default value.
Syntax
fill(v.begin(), v.end(), vector<vector<int>>(y, vector<int>(z, k)));
Here y and z is the number of columns and depth of 3D vector v, and k is the default value by which we initialize 3D vector.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<vector<vector<int>>> v(2,
vector<vector<int>>(2, vector<int>(3)));
// Initialize 3D vector with default value 11
fill(v.begin(), v.end(),
vector<vector<int>>(2, vector<int>(3, 11)));
for (auto i : v) {
for (auto j : i) {
for (auto k : j)
cout << k << " ";
cout << endl;
}
}
return 0;
}
Output11 11 11
11 11 11
11 11 11
11 11 11
By Inserting 2D Vector One by One
The 3D vector can also be initialized by inserting the 2D vector one by one using vector push_back() method.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<vector<vector<int>>> v;
// Create the 2D vector
vector<vector<int>> v1(2, vector<int>(3, 11));
vector<vector<int>> v2(2, vector<int>(3, 9));
// Insert the 2D vector one by one
v.push_back(v1);
v.push_back(v2);
for (auto i : v) {
for (auto j : i) {
for (auto k : j)
cout << k << " ";
cout << endl;
}
}
return 0;
}
Output11 11 11
11 11 11
9 9 9
9 9 9
Using Another 3D Vector
The 3D vector can also be initialized with another 3D vector by copying all elements with the help of copy constructor.
Syntax
vector<vector<vector<int>>> v1(v2);
where v1 and v2 is the name of 3D vector.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<vector<vector<int>>> v2 = {{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}}};
// Initialize 3D vector from another 3D vector
vector<vector<vector<int>>> v1(v2);
for (auto i : v1) {
for (auto j : i) {
for (auto k : j)
cout << k << " ";
cout << endl;
}
}
return 0;
}
Output1 2 3
4 5 6
7 8 9
10 11 12
Similar Reads
How to Initialize 2D Vector in C++? Initializing a 2D vector refers to the process of assigning initial values to the elements of a 2D vector. In this article, we will learn different methods to initialize a 2D vector in C++.The simplest way to initialize a 2D vector is by passing the elements inside an initializer list. This method i
3 min read
How to Initialize a Vector with Zero in C++? Initializing a vector with value zero means assigning the initial value 0 to all elements of vector. In this article, we will learn the different methods to initialize the vector with zero in C++.The simplest method to initialize a vector with zeros is by using vector constructor. Let's take a look
2 min read
How to Initialize Vector of Char Arrays in C++? In C++, a vector is a dynamic array that can grow and shrink in size. A char array is a sequence of characters, typically used to represent strings. In this article, we will learn how to initialize a vector of char arrays in C++. Example: myVector: {âappleâ, âbananaâ, âcherryâ}Initializing a Vector
2 min read
How to Iterate 2D Vector in C++? Iterating or traversing a 2D vector means accessing each element of the 2D vector sequentially. In this article, we will explore different methods to iterate over a 2D vector in C++.The simplest way to iterate a 2D vector is by using a range-based for loop. Let's take a look at a simple example that
3 min read
How to Initialize a Deque from a Vector in C++? In C++, the Standard Template Library(STL) provides a container deque also known as a double-ended queue where insertion and deletions are possible from both ends. On the other hand, vectors are dynamic containers that can resize themselves during the insertion or deletion of elements. In this artic
2 min read