Range-Based For
suggest changeA range-based for loop iterates over a range without an explicit numeric index or iterator. The std::vector below uses class template argument deduction, which is C++17:
#include <iostream>
#include <vector>
int main() {
std::vector values{0.4F, 12.5F, 16.234F};
for (float value : values) {
std::cout << value << ' ';
}
std::cout << '\n';
}
This will iterate over every element in values, with value getting the value of the current element. The following statement:
for (for-range-declaration : for-range-initializer ) statement
is equivalent to:
{
auto&& __range = for-range-initializer;
auto __begin = begin-expr, __end = end-expr;
for (; __begin != __end; ++__begin) {
for-range-declaration = *__begin;
statement
}
}
Since C++17, the begin and end expressions may have different types:
{
auto&& __range = for-range-initializer;
auto __begin = begin-expr;
auto __end = end-expr; // end is allowed to be a different type than begin in C++17
for (; __begin != __end; ++__begin) {
for-range-declaration = *__begin;
statement
}
}
This permits sentinel-based ranges whose end marker has a different type from its iterator.
In this case, our loop is equivalent to:
{
auto&& __range = values;
auto __begin = values.begin(), __end = values.end();
for (; __begin != __end; ++__begin) {
float value = *__begin;
std::cout << value << ' ';
}
}
value is a copy of the current element. If elements are expensive to copy and the loop does not modify them, prefer const auto&. You do not have to use auto; an explicitly written type is also valid when the element can initialize it.
If you need access to the iterator, range-based for cannot help you (not without some effort, at least).
If you wish to reference it, you may do so:
std::vector<float> v{0.4F, 12.5F, 16.234F};
for (float& val : v)
{
std::cout << val << " ";
}
You could iterate on const reference if you have const container:
const std::vector<float> v{0.4F, 12.5F, 16.234F};
for (const float& val : v)
{
std::cout << val << " ";
}
One would use forwarding references when the sequence iterator returns a proxy object and you need to operate on that object in a non-const way. Note: it will most likely confuse readers of your code.
std::vector<bool> v(10);
for (auto&& val : v)
{
val = true;
}
The “range” type provided to range-based for can be one of the following:
-
Built-in arrays:
float arr[] = {0.4f, 12.5f, 16.234f}; for(auto val: arr) { std::cout << val << " "; }A pointer does not retain an array's bound, so a dynamically allocated array is not itself a range:
float* arr = new float[3]{0.4f, 12.5f, 16.234f}; for (auto val : arr) // Error: arr is a pointer, not a range. { std::cout << val << " "; } -
Any type which has member functions
begin()andend(), which return iterators to the elements of the type. The standard library containers qualify, but user-defined types can be used as well:#include <iostream> struct Rng { float arr[3]; // pointers are iterators const float* begin() const {return &arr[0];} const float* end() const {return &arr[3];} float* begin() {return &arr[0];} float* end() {return &arr[3];} }; int main() { Rng rng = {{0.4f, 12.5f, 16.234f}}; for (auto val : rng) { std::cout << val << " "; } } -
Any type for which non-member
begin(range)andend(range)functions can be found by argument-dependent lookup. This is useful when the range type itself cannot or should not be modified:#include <iostream> namespace Mine { struct Rng {float arr[3];}; // pointers are iterators const float* begin(const Rng &rng) {return &rng.arr[0];} const float* end(const Rng &rng) {return &rng.arr[3];} float* begin(Rng &rng) {return &rng.arr[0];} float* end(Rng &rng) {return &rng.arr[3];} } int main() { Mine::Rng rng = {{0.4f, 12.5f, 16.234f}}; for (auto val : rng) { std::cout << val << " "; } }