Notice that an element is only removed from the list if it is equal to the element immediately preceding it. Thus, this function is specially useful for sorted lists.
For the second version, accepting a binary predicate, a specific comparison function to determine the "uniqueness" of an element can be specified. In fact, any behavior can be implemented (and not only a plain comparison), but notice that the function will call binary_pred(*i,*(i-1)) for all pairs of elements (where i is an iterator to an element) and remove i from the list if the predicate returns true.
The elements removed have their destructors called and their iterators and references become invalid.
A global algorithm function, unique, exists with a similar behavior but operating between two iterators.
Paramaters
- BinaryPredicate
- Binary predicate that, taking two values of the same type than those contained in the list object, returns true to remove from the container the element passed as first argument, and false otherwise.
Return value
noneExample
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
Output:
mylist contains: 2.72 12.15 72.25 |
Complexity
Linear in size-1.See also
| list::remove | Remove elements with specific value (public member function) |
| list::remove_if | Remove elements fulfilling condition (public member function template) |
| list::erase | Erase elements (public member function) |
| list::pop_back | Delete last element (public member function) |
| list::pop_front | Delete first element (public member function) |
