map.rbegin() returns an iterator equal to std::reverse_iterator(map.end());
The problem occurs when dereferencing the reverse iterator. When you reverse_iterator , you actually get the value from the iterator before it is stored inside reverse_iterator . This may seem strange, but there are good reasons, and it is inevitable. This is so as to arrange the end of the range element: An iterator pointing to the end element at the end, if not, points to the last element (and not past it) of the range (this will be the first element in the inverse range). And if the iterator of the first element in the range is reversed, the inverse iterator points to the element before the first element (this would be the element at the end of the inverse range).
That is, in your case, when dereferencing reverse_iter equivalent to doing:
*(--map.end());
Therefore, after the second emplace last map element has changed and dereferencing (--map.end()) (i.e. your reverse_iter ), you will get the new last element on the map.
source share