The new C++11 Standard introduces a simpler for statement that can traverse all elements of a container or other sequence. The syntax form for a range for statement is:
for (declaration:expression) statement
expression must represent a sequence in which each element of the sequence can be converted to the type of the variable. The simplest way to ensure that this type is compatible is to use the auto type specifier, which allows the compiler to help us specify the appropriate type. If you need to perform write operations on elements in a sequence, the loop variable must be declared as a reference type.
Each iteration will redefine the loop control variable and initialize it to the next value in the sequence before the statement is executed.
Example:
vector<int> v = {0,1,2,3,4,5}; // The range variable must be a reference type in order to write to the element for (Auto &r:v) 2;
C + + Prime: Range for statement