Sequential containers::(vectors and lists and deque)
The erase iterator not only invalidates all iterators that point to the deleted element, but also invalidates all iterators after the deleted element, so erase (iter++) cannot be used, but the return value of erase is the next valid iterator, so the
correct method is:: For (iter = C.begin (); ITER! = C.end ();)
iter = C.erase (ITER);
Associative containers::(map and set are more commonly used) The erase iterator is only invalidated by the iterator of the deleted element, but the return value is void, so the iterator is removed in the erase (iter++) manner, with the correct method:
for (iter = C.begin (); ITER! = C.end ();)
C.erase (iter++);
Tips:
In fact, for the list of two ways to work properly
C + + prevents--map/set such as associative containers such as iterator invalidation in STL--vector/list/deque-How to prevent iterators from being invalidated-that is, the use of erase ()