C++ Reference ยท Keywords and Statements
The range-based for loop
Reference for the C++ range-based for loop: iterating containers and arrays, auto vs auto&, structured bindings, init statements and dangling temporaries.
The range-based for loop (C++11) visits every element of a range without an index or explicit iterators. It works on arrays, standard containers, std::string, initializer lists and any type that provides begin() and end() (members or found by argument-dependent lookup).
The element declaration decides whether you get a copy (auto x), a mutable reference (auto& x) or a read-only reference (const auto& x). Choosing the wrong one is the most common mistake: it either copies large objects or silently modifies a copy instead of the container.
for (const auto& item : items) { // use item }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
for (range-declaration : range-initializer) statement for (init-statement range-declaration : range-initializer) statement // C++20
Roughly equivalent to:
{
auto&& __range = range-initializer;
for (auto __it = begin(__range), __end = end(__range); __it != __end; ++__it) {
range-declaration = *__it;
statement
}
}
| Part | Description |
|---|---|
range-declaration |
A declaration of the loop variable, often auto, auto&, const auto& or a structured binding such as const auto& [k, v]. |
range-initializer |
An expression or braced list that names the range. For arrays the bounds are known to the compiler; for classes begin() and end() are used. |
init-statement |
Optional (C++20): a declaration that lives for the whole loop, useful for counters. |
Usage notes
const auto&is the safe default for reading. Useauto&to modify elements andautofor cheap types or when you want a copy.- For a
std::vector<bool>,auto&fails to compile because the element is a proxy; useautoorauto&&. - The range expression is evaluated once. Its temporaries are destroyed at the end of the range initializer before C++23, so
for (x : f().member())could dangle; C++23 extends the lifetime of all temporaries in the range initializer. - Do not add or erase elements of the container being iterated; this can invalidate the hidden iterators.
- Arrays that have decayed to pointers cannot be iterated (their size is lost). Pass a reference to the array, a
std::spanor a container. - Iteration over a
std::mapvisitsstd::pair<const Key, T>. Structured bindings give the parts readable names. - Types need
begin()andend()that can be compared; since C++17 they may return different types (sentinels), which is how manystd::rangesviews work. - To iterate in reverse use
std::views::reverse(C++20).
Related
- for
- while and do-while
- Structured bindings
- auto
- Tutorial: for loops, Vectors