Why does g ++ 4.0 version of the map <T> :: erase (map :: <T> iterator) not return an iterator?

I am migrating a mid-size C ++ project from Visual Studio 2005 to MacOS, Xcode / GCC 4.0.

One of the differences that I just came across is related to erasing an item from the map. In Visual Studio, I can erase the element indicated by the iterator and assign the return value to the iterator to get the position of the next element. Thus, the iterator will not point to some invalid address after erasing.

In other words, in Visual Studio, I could do this:

itor = m_ResourceMap.erase (itor);

In GCC 4.0, the erase function returns void, so I cannot do this. Does this mean that the following map elements are moved one back, so the iterator automatically points to the next element, or does it mean that I need to increase the iterator after that? STL's online documentation is not very concise on this, and Xcode doesn't seem to have it.

Thank you for your help,

Adrian

+3
source share
3 answers

No. You must increase the iterator before erasing. Like this

m_ResourceMap.erase(itor++);

An iterator is invalid by erasure, so you cannot subsequently increase it.

+11
source

For gcc reasons, following the STL standard, I think you'll have to change your code.

void erase(iterator pos)     Associative Container  Erases the element pointed to by pos. 
+3

, DinkumWare ++ erase() , . , DinkumWare, , " ".

STL (, ), . , , , , .

+1

All Articles