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 ...

Get Learning Boost C++ Libraries now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.