To iterate over a list in C++ using std::list, you can use iterators.
In this example,
for std::list.myList
with elements.#include <iostream>
#include <list>
int main() {
std::list<int> myList = {1, 2, 3, 4, 5};
for (auto it = myList.begin(); it != myList.end(); ++it) {
std::cout << *it << std::endl;
}
return 0;
}
1 2 3 4 5
In this tutorial, we learned How to Iterate Over a List in Reverse Order in C++ language with well detailed examples.