public member function
<list>
std::list::remove
void remove (const value_type& val);
Remove elements with specific value
Removes from the container all the elements that compare equal to val. This calls the destructor of these objects and reduces the container size by the number of elements removed.
Unlike member function list::erase, which erases elements by their position (using an iterator), this function (list::remove) removes elements by their value.
A similar function, list::remove_if, exists, which allows for a condition other than an equality comparison to determine whether an element is removed.
Parameters
- val
- Value of the elements to be removed.
Member type value_type is the type of the elements in the container, defined in list as an alias of its first template parameter (T).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
// remove from list
#include <iostream>
#include <list>
int main ()
{
int myints[]= {17,89,7,14};
std::list<int> mylist (myints,myints+4);
mylist.remove(89);
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
| |
Output:
Complexity
Linear in container size (comparisons).
Iterator validity
Iterators, pointers and references referring to elements removed by the function are invalidated.
All other iterators, pointers and reference keep their validity.
Data races
The container is modified.
The elements removed are modified. Concurrently accessing or modifying other elements is safe, although iterating through the container is not.
Exception safety
If the equality comparison between elements is guaranteed to not throw, the function never throws exceptions (no-throw guarantee).
Otherwise, if an exception is thrown, the container is left in a valid state (basic guarantee).