Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | language
Revision as of 14:34, 15 January 2013 by Jyasskin (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function declaration
inline specifier
Exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
decltype (C++11)
auto (C++11)
alignas (C++11)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous
 

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

Syntax

for ( Template:sparam : Template:sparam) Template:sparam

Explanation

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

{
auto && __range = Template:sparam ;
for (auto __begin = begin_expr,
__end = end_expr;
__begin != __end; ++__begin) {
Template:sparam = *__begin;
Template:sparam
}

}

The Template:sparam 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 Template:sparam.

The begin_expr and end_expr are defined to be either:

  • If (__range) is an array, then (__range) and (__range + __bound), where __bound is the array bound
  • If (__range) is a class and has either a begin or end member (or both), then begin_expr is __range.begin() and end_expr is __range.end();
  • Otherwise, begin(__range) and end(__range), which are found based on argument-dependent lookup rules with std as an associated namespace.

If Template:sparam returns a temporary, its lifetime is extended until the end of the loop, as indicated by binding to the rvalue reference __range, but beware that temporaries nested inside Template:sparam do not get extended lifetimes.

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.

Keywords

for

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