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
Use range-v3
. Range-v3
is the next generation range library designed and implemented by ISO C++ Committee member Eric Niebler, and is intended and expected to be merged in the C++ standard in the future.
By Using range-v3
OP's problem can be solved easily:
using ranges::v3::view::zip;
using ranges::v3::view::ints;
for(auto &&[i, idx]: zip(storedValues, ints(0u))){
std::cout << idx << ": " << i.function() << '\n';
}
You will need a compiler that support C++17 or later to compile this piece of code, not only for the structured binding syntax, but also for the fact that the return type of begin
and end
function for the return value of ranges::v3::view::zip
differ.
You can see the online example here. The documentation of range-v3
is here and the source code itself is hosted here. You can also have a look at here if you are using MSVC compilers.