In an unordered_set, the order of inserts does not necessarily correspond to the order that you will get when the set is iterated (hence the name “unordered”). Part of the reason why a bi-directional iterator is not supported(using a — operator) in this data structure is because being able to go backwards/forwards on an unordered_set makes no difference when you don’t know what the order of the elements that you will get out of it.
The order of inserts that you have created does not dictate the order you will get when you iterate (Inserting “9” first doesn’t mean s.end() would return a “9”). This is because what dictates that order solely depends on how that set calculates the hash value of each object you insert, similar to a hash table (http://en.wikipedia.org/wiki/Hash_table). Hence, you cannot reliably use this set to replicate a “stack”, as this is not what this particular data structure is meant to be used for.
There are other C++ STL data structures that you can use to preserve order such as http://www.cplusplus.com/reference/stack/.
solved Get Last element from unordered_set [closed]