'How to iterate an unordered_set from the end to the begin

I want to iterate an unordered_set from the end to the begin:

unordered_set<Expression*> BlocExpressions;

for(auto it=BlocExpressions.end(); it != BlocExpressions.begin(); it--)
    //do some work

But there is no operator-- declared.

So, should I code the operator--, or is there a way to do that?

Solution 1:[1]

For std::unordered_set, the order in which you iterate through the elements does not matter. Saying that, you could just imagine the order is random. You get no particular order regardless you do a forward iteration or backward iteration. That's why it provides no reverse iterator nor provides the -- operator overload for normal iterator. Forward and backward iterations have the same semantics here: to iterate in a random order.

Solution 2:[2]

I can't understand why you use words "end" and "begin" for unordered_set. unordered_set does not have particular order. You can iterate all elements by using iterator object. If you need order in the set, you should use other container, for example std::set

Solution 3:[3]

I also find it curious why there is no rbegin() and rend(). I am using unordered_set to add random numbers (unordered) to represent a certain path. That would be great.

The solution I have found, it might help someone else is the following one. Adding the decrement in the first argument of the for loop: auto it = --BlocExpressions.end()

unordered_set<Expression*> BlocExpressions;

for(auto it = --BlocExpressions.end(); ; it--){   

    //do some work

    // This will include the last item (which is BlocExpressions.begin())
    if(it == BlocExpressions.begin()){


This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lingxi
Solution 2 Andrew Romanov
Solution 3