| Looping through a container In C++, looping through a sequence container c can be done using indexes as follows: While simple, such writings are subject to common semantic errors, like wrong comparison operator, or wrong indexing variable: Looping can also be achieved for all containers using iterators, with similar drawbacks: C++11 introduced range-based for loops and auto keyword, allowing the code to become: Here the only parameters are the container c, and a variable x to hold the current value. This prevents the semantics errors previously pointed. According to the C++11 standard, the underlying implementation is equivalent to: In such implementation, the expression auto begin = c.begin(), end = c.end(); forces begin and end to be of the same type, while end is never incremented, nor dereferenced. So the range-based for loop only works for containers defined by a pair iterator/iterator. The C++17 standard relaxes this constraint by changing the implementation to: Here begin and end are allowed to be of different types, as long as they can be compared for inequality. This allows to loop through more containers, e.g. a container defined by a pair iterator/sentinel. |