In C++ using std::list, you can iterate over a Two-Dimensional List using nested for loops. The outer loop iterates over rows, and the inner loop iterates over elements in each row. This allows you to access and process each element in the 2D list.
In this example,
#include <iostream>
#include <list>
int main() {
std::list<std::list<int>> matrix = {{1, 2, 3}, {4, 5, 6}};
for (auto row : matrix) {
for (int val : row) {
std::cout << val << "\n";
}
}
return 0;
}
1 2 3 4 5 6
In this example,
#include <iostream>
#include <list>
int main() {
std::list<std::list<std::string>> matrix = { {"apple", "banana"}, {"cherry", "orange"} };
for (auto row : matrix) {
for (std::string str : row) {
std::cout << str << "\n";
}
}
return 0;
}
apple banana cherry orange
In this tutorial, we learned How to Iterate over a Two Dimensional List in C++ language with well detailed examples.