STL Notes (4) About Erase,remove
The elements you want to erase are easy to identify. They are elements of the original interval starting from the "New logical end point" of the interval to the end of the interval true. To get rid of those elements, all you have to do is invoke the interval form of the erase with those two iterators (see clause 5). Because the remove itself easily returns an iterator to the new logical endpoint of the interval, the call is straightforward:
Vector<int> v; As before
V.erase (Remove (V.begin (), V.end (), V.end ()); Really delete all
Element equal to 99
cout << v.size (); Return now 7
It is a common practice to pass the return value of remove as the first argument in the Erase interval form. In fact, remove and erase are intimate alliances, both of which are integrated into the list member function remove. This is the only function in STL that removes an element in the name of remove and can be contained in the device:
List<int> Li; Create a list
Put some values in there.
Li.remove (99); Remove all elements equal to 99:
Really delete the element,
So the size of it may have changed
Frankly speaking, calling this remove function is a contradiction in STL. A similar function in the associative container is called the erase,list Remove, which can also be called erase. But it doesn't, so we all have to get used to it. The world we are in is not the best of all possible worlds, but it is where we are. (Add a point, clause 44 indicates that for list, calling the Remove member function is more efficient than applying erase-remove idioms.) )
Once you know that remove can't "really" remove something from a container, it's taken for granted that it is used in conjunction with erase. The only other thing you have to remember is that the remove is not the only algorithm for this situation. There are two other "remove-like" algorithms: remove_if and unique.
The similarity between remove and remove_if is straightforward. So I'm not going to say it, but the unique behavior is like remove. It is used to remove objects from a range (adjacent repeating values) without accessing the container holding the interval element. As a result, if you really want to remove the element from the container, you must also call the unique and Erase,unique in the list also similar to remove. Just as List::remove really deletes things (and is much more efficient than erase-remove idioms). List::unique also really deletes adjacent duplicate values (also more efficient than erase-unique).
(Note: "C + + standard library" 111 page 5.6 Section has a detailed explanation of remove)
STL Notes (4) About Erase,remove