Example 1 — begin() and end() in a Loop
Walk through a list with an iterator, from the first element to the end.
#include <iostream>
#include <list>
int main() {
std::list<int> l = {10, 20, 30};
for (auto it = l.begin(); it != l.end(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
return 0;
} How It Works
l.begin()gives an iterator that points at the node with10.*itreads the element, and++itfollows the link to the next node. The loop stops whenitbecomes equal tol.end().- The three elements are printed in order. A range-based
forloop does exactly this behind the scenes.

