Skip to main content
GameDev.net gamedev.net
🔒 Locked

No std :: map :: erase version for reverse_iterator? :-/

Started by Koshmaar Sep 18, 2005 at 3:13 PM 3 replies 2.3k views
Original Post
Koshmaar
Koshmaar
Hi, my problems goes like this: I have a std :: map and reverse_iterator is moving through its elements. Everything compiles and works fine, till I want to erase element that is pointed by that reverse iterator - I got compile errors saying that there's no erase() function in std :: map which would take reverse_iterator. However, as you know :-) there is erase version for normal iterator. I feel that's not very fair (maybe it's a pitfall of design?), but since it's how STL was designed, I think that there's nothing I can do about it directly - I have to workaround it [razz]. So, my question is - what would be the best performance efficient way of fixing this issue? I thought about dereferencing element that reverse_iterator was facing , then using its value to find() normal iterator to that element and then - erasing element through new iterator. Any ideas?
Fruny
Fruny
You can get a regular iterator out of a reverse iterator by using its base() member function.

std::map<int,int> m;std::map<int,int>::reverse_iterator r_itor = m.rbegin();std::map<int,int>::iterator itor = r_itor.base();
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Enigma
Enigma
But note that the erasing using the iterator returned by base will erase the following element. base gives the "corresponding" iterator, which is the iterator on which a call to insert would yield the same result. If you wish to call erase you either have to increment the reverse iterator and then call base or call base and then decrement the resulting iterator.

Enigma
iMalc
iMalc
I ran into the same thing a week ago. Now I wish I had posted on here myself asking the same thing. I ended up not needing to do it a bit later on though.
Thanks for the answers anyway!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.