Updated April 17, 2023
Introduction to C++ 3D vector
The 3D vector is a vector of vectors, like the 3D array. It stores elements in the three dimensions. It can be declared and assign values the same as a 3D matrix. The 3D Vector is a dynamic which has the capability to resize itself automatically when an element is to be inserted or delete. The 3D vector storage is being handled automatically by the container automatically. In this topic, we are going to learn about C++ 3D vector.
Syntax
The declaration syntax of the 3D vector in C++:
std :: vector< std :: vector< std :: vector< data_type > > > vectName;
The initialization syntax of the 3D vector in C++:
vectName[index1][ index2][ index3] = value;
Or
The syntax to declare and initialize at the same time for 3D vector in C++:
std :: vector< std :: vector< std :: vector< data_type > > > vectName( size, std :: vector< std :: vector<data_type> > ( size, std :: vector< data_type >( size, value)));
Parameters:
- data_type: This specifies the type of vector we want to create. So, the data type can be int, float, string etc.
- size: This specifies the size of the vector we want to create; in each dimension of the vector, it can be the same or different.
- value: This specifies the initialize value for the vector elements and all elements initialize by this value.
Working of the 3D vector in C++
- A 3D vector is actually a dynamic 3D array which can insert and delete elements by resizing itself. Same as the 3D array, the 3D vector also defines what type of it is so that we can create the int type, string type, float type and all different types of the 3D vector.
- The 3D vector stores the elements in three dimensions as block size, row size and column size, which is represented by using the three different subscripts.
- By using these subscripts or indexes, we can store the values into the 3D vector and also, we can access the values stored in the vector.
Examples
Example of creating the 3D vector with value 1 to all elements of the vector.
Next, we write the C++ code to understand the 3D vector more clearly with the following example, where the 3D vector of 2*2*3 sizes and initialize all the elements with value 1.
Example #1
Code:
#include <iostream>
// for 3D vector
#include <vector>
using namespace std;
int main()
{
// Initializing 3D vector "vect3d" with
// values 1
//here creating the vector of 1D of 2 size, 2D of 2 size and 3D of 3 size
std::vector<std::vector<std::vector<int> > > vect3d(2, std::vector<std::vector<int> > (2, std::vector<int>(3,1)));
// Displaying the 3D vector by using 3 iterators
for (int i = 0; i < vect3d.size(); i++) {
cout << "Elements at block "
<< i << ": ";
// Displaying element at each column,
// 0 is the starting iterator,
// size() is the ending iterator
for (int j = 0; j != vect3d[i].size(); j++) {
cout << "Elements at row "
<< j << ": ";
for (int k = 0; k != vect3d[i][j].size(); k++) {
// use all indexes to get the values store in the vector
cout << vect3d[i][j][k]<< ' ';
}
cout << endl;
}
}
return 0;
}
Output:
As in the above program, the 3D vector is declared with the different size for each dimension as 2 for block size, 2 for row size and 3 for the column size, so the number of elements in this vector will be 12 elements. And at the time of declaration itself, the vector is initialized with the value 1 for all the 12 elements of the vector, as we can see in the output.
Example of creating the 3D vector with the value entered by the user.
Next, we write the c++ code to understand the 3D vector, where we will create the 3D vector of 2*2*3 sizes and store the user-provided values into the vector.
Example #2
Code:
#include <iostream>
// for 3D vector
#include <vector>
using namespace std;
int main()
{
// Initializing 3D vector "vect3d" with
// user provided values
//here creating the vector of 1D of 2 size, 2D of 2 size and 3D of 3 size
std::vector<std::vector<std::vector<int> > > vect3d(2, std::vector<std::vector<int> > (2, std::vector<int>(3)));
// Inserting elements into the vector
for (int i = 0; i < vect3d.size(); i++) {
cout << "Elements at block "
<< i << ": ";
// Inserting element at each column,
// 0 is the starting iterator,
// size() is the ending iterator
for (int j = 0; j != vect3d[i].size(); j++) {
cout << "Elements at row "
<< j << ": ";
for (int k = 0; k != vect3d[i][j].size(); k++) {
cout<<"\nEnter number: ";
// use all indexes to insert the values into the vector
cin >> vect3d[i][j][k];
}
cout << endl;
}
}
// Displaying the 3D vector by using 3 iterator
cout << "The vectore values are: " <<" ";
for (int i = 0; i < vect3d.size(); i++) {
// Displaying element at each column,
// 0 is the starting iterator,
// size() is the ending iterator
for (int j = 0; j != vect3d[i].size(); j++) {
for (int k = 0; k != vect3d[i][j].size(); k++) {
// use all indexes to get the values store in the vector
cout << vect3d[i][j][k]<< ' ';
}
cout << endl;
}
}
return 0;
}
Output:
As in the above program, the 3D vector is declared with the different size for each dimension as 2 for block size, 2 for row size and 3 for the column size. And later in the code, the user-provided values are storing into the vector with the help of three indexes or iterators, and in the same way, the vector is displaying with the help of three indexes.
Conclusion
The vector is a built-in sequence container in C++, which is defined in the <vector> header. The 3D vector is a vector of vectors, like the 3D arrays, and by using the iterators, we can access the vector elements.
Recommended Articles
This is a guide to C++ 3D vector. Here we discuss the introduction, working of the 3D vector in C++ along with examples, respectively. You may also have a look at the following articles to learn more –