I am adding some functionality to a previously built code. I have to loop over a list is steps of 2 and process the elements elements differently.
Here's my attempt.
for(auto list_it = std::next(list.begin()); list_it != list.end() ; std::advance(list_it,2)){
// Do something
}
I am initializing list_it
to point to the second element using next(list.begin())
Each iteration it moves forward by 2 advance(list_it,2))
.
[Q]
I am unsure how to test the loop exit condition.
list_it != list.end()
might not work if we skip over list.end()
when advancing forward by 2.
Is there any standard way to deal with this issue? I might check skipped iterators for being list.end()
, but I don't see this as a nice, scalable way with larger step sizes. (unless it turns out to be the only way).