C++17 and structured bindings makes this look OK - certainly better than some ugly mutable lambda with a local [i = 0](Element&) mutable
or whatever I've done before admitting that probably not everything should be shoehorned into for_each()
et al. - and than other solutions that require a counter with scope outside the for
loop.
for (auto [it, end, i] = std::tuple{container.cbegin(), container.cend(), 0};
it != end; ++it, ++i)
{
// something that needs both `it` and `i`ndex
}
You could make this generic, if you use this pattern often enough:
template <typename Container>
auto
its_and_idx(Container&& container)
{
using std::begin, std::end;
return std::tuple{begin(container), end(container), 0};
}
// ...
for (auto [it, end, i] = its_and_idx(foo); it != end; ++it, ++i)
{
// something
}
C++ Standard proposal P2164 proposes to add views::enumerate
, which would provide a view of a range giving both reference-to-element and index-of-element to a user iterating it.
We propose a view enumerate
whose value type is a struct
with 2 members index
and value
representing respectively the position and value of the elements in the adapted range.
[ . . .]
This feature exists in some form in Python, Rust, Go (backed into the language), and in many C++ libraries: ranges-v3
, folly
, boost::ranges
(indexed
).
The existence of this feature or lack thereof is the subject of recurring stackoverflow questions.
Hey, look! We're famous.