when and how are iterators invalidated in a map when using the erase method ?
for example :
std :: map < int , int > aMap ;
aMap [ 33 ] = 1 ;
aMap [ 42 ] = 10000 ;
aMap [ 69 ] = 100 ;
aMap [ 666 ] = -1 ;
std :: map < int , int > :: iterator itEnd = aMap.lower_bound ( 50 ) ;
for ( std :: map < int , int > :: iterator it = aMap.begin ( ) ;
it != itEnd ;
// no-op
)
{
aMap.erase ( it ++ ) ;
}
the erased iterator will surely become invalid (it's incremented while still valid) but what about the others?
if I'm not wrong the standard says that a map has to be a balanced binary tree or a structure with equivalent key-search complexity
in case the map is implemented with a tree, can I assume that not erased iterators remain valid ?
what about other possible ways to implement a map ?
Every iterator and reference after the point of erasing is invalidated.
All iterators and the references are invalidated if the inserted item is not inserted at the end of the deque. If the items are deleted from any of the position except the end position, then all iterators will be invalidated. Same as like insert or erase.
Map has the important property that inserting a new element into a map does not invalidate iterators that point to existing elements. Erasing an element from a map also does not invalidate any iterators, except, of course, for iterators that actually point to the element that is being erased.
No, they should not get invalidated after a move operation.
Only the erased iterator is invalid, the rest are guaranteed by the standard to remain valid.
See Iterator invalidation rules
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With