I need to iterate over a vector from the end to the beginnig. The "correct" way is
for(std::vector<SomeT>::reverse_iterator rit = v.rbegin(); rit != v.rend(); ++rit) { //do Something }
When doSomething involves knowing the actual index, then some calculations need to be done with rit to obtain it, like index = v.size() - 1 - (rit - v.rbegin)
If the index is needed anyway, then I strongly believe it is better to iterate using that index
for(int i = v.size() - 1; i >= 0; --i) { //do something with v[i] and i; }
This gives a warning that i
is signed and v.size()
is unsigned. Changing to
for(unsigned i = v.size() - 1; i >= 0; --i)
is just functionally wrong, because this is essentially an endless loop :)
What is an aesthetically good way to do what I want to do which
I hope I am not looking for something that doesn't exist :)
C++ Iterators Reverse Iterators A reverse iterator is made from a bidirectional, or random access iterator which it keeps as a member which can be accessed through base() . To iterate backwards use rbegin() and rend() as the iterators for the end of the collection, and the start of the collection respectively.
C++ set rbegin() C++ set rbegin() function is used to return a reverse iterator referring to the last element of the set container. A reverse iterator of set moves in reverse direction and incrementing it until it reaches to the beginning (First element) of the set container.
The rend() function is an inbuilt function in C++ STL which returns a reverse iterator pointing to the theoretical element right before the first key-value pair in the map(which is considered its reverse end). Syntax: map_name.rend()
As you've noted, the problem with a condition of i >= 0
when it's unsigned is that the condition is always true. Instead of subtracting 1 when you initialize i
and then again after each iteration, subtract 1 after checking the loop condition:
for (unsigned i = v.size(); i-- > 0; )
I like this style for several reasons:
i
will wrap around to UINT_MAX
at the end of the loop, it doesn't rely on that behavior — it would work the same if the types were signed. Relying on unsigned wraparound feels like a bit of a hack to me.size()
exactly once.>=
. Whenever I see that operator in a for
loop, I have to re-read it to make sure there isn't an off-by-one error.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With