To create a Three Dimensional Array in C++, you can use nested vectors or arrays.
In this example,
// Creating a 3D Array with Nested Vectors
#include <iostream>
#include <vector>
int main() {
std::vector<std::vector<std::vector<int>>> threeDArray(3, std::vector<std::vector<int>>(2, std::vector<int>(4)));
// Modify an element
threeDArray[1][0][2] = 5;
// Print the array
for (auto& outer : threeDArray) {
for (auto& middle : outer) {
for (int value : middle) {
std::cout << value << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
return 0;
}
0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
In this tutorial, we learned How to create a Three Dimensional Array in C++ language with well detailed examples.