It is very easy to delete an element in STD: List. Use the erase method directly,CodeAs follows:
For ( ITER = List. Begin ( ) ; ITER ! = List. End ( ) ; ) {
If ( Shoulddelete ( * ITER) )
ITER = List. Erase ( ITER ) ;
Else
++ ITER ;
}
Or simpler.
List. Erase ( STD:: Remove_if ( List. Begin ( ) , List. End ( ) , Shoulddelete ) , List_end ( ) ) ;
However, according to the definition of void erase (iterator POS) in stl std: map, this erase does not return the iterator of the next element. Therefore, the STD: list method cannot be used.
The truth is that 'erase' in Associative containers doesn' t invalidate any iterators should t those that point to elements being erased (That's also true for 'sid: list '). for this reason, you don't really need 'map: erase 'to return an iterator. just do this
For ( ITER = Map.Begin ( ) ; ITER ! = Map. End ( ) ; ) {
If ( Shoulddelete( * ITER ) )
Map. Erase ( ITER ++ ) ;
Else
++ ITER ;
}
Of course, this method is also suitable for STD: List and so on.