Range-based for loops can be used in various ways:
- By committing to a specific type for the elements of the sequence:
auto rates = getRates();
for (int rate : rates)
std::cout << rate << std::endl;
for (int& rate : rates)
rate *= 2;
- By not specifying a type and letting the compiler deduce it:
for (auto&& rate : getRates())
std::cout << rate << std::endl;
for (auto & rate : rates)
rate *= 2;
for (auto const & rate : rates)
std::cout << rate << std::endl;
- By using structured bindings and decomposition declaration in C++17:
for (auto&& [rate, flag] : getRates2())
std::cout << rate << std::endl;