Standard Initialization of a 3D vector
Standard initialization of a 3D vector is a method where we initialize by declaring and then inserting elements using the push_back( ) function.
Syntax:
vector<vector<vector<data_type>>> vector_name;
Example:
C++
// C++ program to initialise // 3D vector using Standard // initialization of a 3D vector #include <iostream> #include <vector> using namespace std; int main() { // Initialising an empty 3D vector vector<vector<vector< int > > > v; // Adding values to the vector v.push_back({ { 1, 2, 3 }, { 3, 2, 1 } }); v.push_back({ { 4, 5, 6 }, { 6, 5, 4 } }); // Printing the 3d vector 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; } } return 0; } |
Output
1 2 3 3 2 1 4 5 6 6 5 4
How to Initialize 3D Vector in C++ STL?
Prerequisite: Vector in C++
Vectors in C++ are the same as arrays with dynamic sizes having the ability to resize themselves, we can insert and remove elements from the end.