Range-based for-loops
Range-based for-loops are another syntactic convenience introduced in C++11. Range-based for-loops allow you to iterate through a sequence of values like arrays, containers, iterator ranges, and so on, without having to explicitly specify boundary conditions. It makes iterating less error-prone by obviating the need to specify boundary conditions.
The general syntax for range-based for-loop is:
for (range-declaration : sequence-expression) { statements; }
The sequence expression identifies a sequence of values like an array or a container, that is to be iterated through. The range declaration identifies a variable that would represent each element from the sequence in successive iterations of the loop. Range-based for-loops automatically recognize arrays, brace-enclosed sequences of expressions, and containers with begin
and end
member functions that return forward iterators. To iterate through all elements in an array, you write this:
T arr[N];
...
for (const auto&...