To create a three-dimensional list in C++ using std::list, you can nest lists within lists to represent the multi-dimensional structure.
In this example,
#include <iostream>
#include <list>
int main() {
std::list<std::list<std::list<int>>> threeDList;
for (int i = 0; i < 3; ++i) {
std::list<std::list<int>> plane;
for (int j = 0; j < 2; ++j) {
std::list<int> row;
for (int k = 0; k < 2; ++k) {
row.push_back(i * 4 + j * 2 + k + 1);
}
plane.push_back(row);
}
threeDList.push_back(plane);
}
for (const auto& plane : threeDList) {
for (const auto& row : plane) {
for (const auto& element : row) {
std::cout << element << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12
In this tutorial, we learned How to create a Three Dimensional List in C++ language with well detailed examples.