Every reverse_iterator
has a base()
function, that returns "normalized" iterator. You could use it like this:
auto e = ms.rbegin();
ms.erase((++e).base());
We must increment e
before getting underlying iterator, because the base iterator refers to the element that is next to the element the reverse_iterator is pointing to.
Another solution is using std::prev
function:
ms.erase(std::prev(ms.end())); // since C++11
If C++11 is not an option for you, you could implement it yourself:
// copy-pasted from cppreference
template<class BidirIt>
BidirIt prev(BidirIt it,
typename std::iterator_traits<BidirIt>::difference_type n = 1)
{
std::advance(it, -n);
return it;
}
It's more simpler, than first, but I leave two solution, because earlier I described first example incorrectly.