How to delete elements from different containers
Remove all objects with specific values in a container:
If the container is a vector, string, or deque, use erase-Remove.
If the container is list, use list: Remove.
If the container is a standard associated container, use its erase member function.
Remove all objects in a container that meet a specific criterion:
If the container is a vector, string, or deque, use a erase-remove_if.
If the container is list, use list: remove_if.
If the container is a standard associated container, write a loop to traverse the container elements. Remember when you pass the iterator to erasePost IncrementIt (after an element in erase, The iterator pointing to this element becomes invalid, and all the iterators that need to point to the next element before erase ).
Assoccontainer <int> C;... For (assoccontainer <int>: iterator I = C. Begin (); I! = C. End ();/* Nothing */)// The third part of the For Loop is empty.{If (badvalue (* I) {C. Erase (I ++ );// For bad values, pass the current I to erase, and then increase I as a side effect;} Else {++ I;// For a good value, only add I}}
Do something in the loop (except Delete objects ):
If the container is a standard sequence container, write a loop to traverse the container elements. RememberUpdate iterator with its return value.
If the container is a standard associated container, write a loop to traverse the container elements. Remember when you pass the iterator to erasePost IncrementIt
Valid STL: Methods for deleting elements from different containers