To iterate over a list in C++ using std::list, you can use an iterator to traverse through the elements of the list.
In this example,
for using std::list.numbers
with integer elements.it
to iterate over the elements of the list.#include <iostream>
#include <list>
int main() {
std::list<int> numbers = {10, 20, 30, 40, 50};
std::list<int>::iterator it;
for (it = numbers.begin(); it != numbers.end(); ++it) {
std::cout << *it << std::endl;
}
return 0;
}
10 20 30 40 50
In this tutorial, we learned How to Iterate Over a List in C++ language with well detailed examples.