I have a vector of objects and am iterating through it using a range-for loop. I am using it to print a function from the object, like this:
vector
You can use the enumerate
view of range-v3:
std::vector storedValues;
for (auto const& [idx, value] : storedValues | ranges::view::enumerate) {
std::cout << idx << ": " << value << '\n';
}
In C++20 will introduce additional initializations in range-for loops:
std::vector storedValues;
for (size_t idx = 0; auto value : storedValues) {
std::cout << idx << ": " << value << '\n';
++idx;
}