Reputation: 49
in for(auto e : elem)
I am trying to figure how to access element e-1. while in pre- C++ 11 it would have just been elem[e-1]
I am sure that has to be a c++ 11 equivalent.
Upvotes: 0
Views: 75
Reputation: 217235
With range-v3, you might do
for (auto [p, n] : ranges::view::zip(v, v | ranges::view::drop(1))) {
// ...
}
Upvotes: 1
Reputation: 11340
Those two do vastly different things:
for(auto e : elem)
is syntactic sugar for a loop the iterates through the whole collection elem
, whereas
elem[e - 1]
is accessing a single element of elem
at the given index e - 1
.
C++11 didn't remove access by index nor is it considered bad practice, because sometimes you don't have any other choice for implementing an algorithm.
Upvotes: 0