Namespaces
Variants
Views
Actions

Range-based for loop (since C++11)

From cppreference.com
 
 
C++ language
General topics
Preprocessor
Comments
Keywords
ASCII chart
Escape sequences
History of C++
Flow control
Conditional execution statements
Iteration statements
for loop
range-for loop (C++11)
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

Executes a for loop over a range.

Used as a more readable equivalent to the traditional for loop operating over a range of values, for example, from some container or list.

Contents

[edit] Syntax

for ( range_declaration : range_expression) loop_statement

[edit] Explanation

The above syntax produces code similar to the following (__range, __begin and __end are for exposition only):

{
auto && __range = range_expression ;
auto __begin = begin_expr(__range);
auto __end = end_expr(__range);
for (;__begin != __end; ++__begin) {
range_declaration = *__begin;
loop_statement
}

}

The range_expression is evaluated to determine the sequence or range will be iterated over. Each element of the sequence is dereferenced, and assigned to the variable using the type and name given in the range_declaration.

The begin_expr and end_expr are defined to be either:

  • (__range) and (__range + __bound) for array types, where __bound is the array bound
  • begin(__range) and end(__range), which are found based on argument-lookup rules. For standard containers this ends up being equivalent to std::begin and std::end which in turn calls __range.begin() and __range.end().

If range_expression returns a temporary, its lifetime is extended until the end of the loop, as indicated by binding to the rvalue reference __range.

Just as with a traditional loop, break statement can be used to exit the loop early and continue statement can be used to restart the loop with the next element.

[edit] Keywords

for

[edit] Example

#include <iostream>
#include <vector>
 
int main() 
{
    std::vector<int> v = {0, 1, 2, 3, 4, 5};
 
    for (int &i : v) // access by reference (const allowed)
        std::cout << i << ' ';
 
    std::cout << '\n';
 
    for (auto i : v) // compiler uses type inference to determine the right type
        std::cout << i << ' ';
 
    std::cout << '\n';
 
    for (int i : v) // access by value as well
        std::cout << i << ' ';
 
    std::cout << '\n';
}

Output:

0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5