To create a Two-Dimensional List in C++ using std::list, you can use nested lists or lists of lists to represent rows and columns.
In this example,
#include <iostream>
#include <list>
int main() {
std::list<std::list<int>> matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (const auto& row : matrix) {
for (const auto& element : row) {
std::cout << element << ' ';
}
std::cout << '\n';
}
return 0;
}
1 2 3 4 5 6 7 8 9
In this example,
#include <iostream>
#include <list>
#include <string>
int main() {
std::list<std::list<std::string>> matrix = {
{"apple", "banana", "cherry"},
{"orange", "grape", "kiwi"}
};
for (const auto& row : matrix) {
for (const auto& element : row) {
std::cout << element << ' ';
}
std::cout << '\n';
}
return 0;
}
apple banana cherry orange grape kiwi
In this tutorial, we learned How to create a Two Dimensional List in C++ language with well detailed examples.