public member function
std::list::remove
<list>
void remove ( const T& value );
Remove elements with specific value
Removes from the
list all the elements with a specific
value. This calls the destructor of these objects and reduces the list
size by the amount of elements removed.
Unlike member function
list::erase, which erases elements by their position (iterator), this function (
list::remove) removes elements by their value.
A similar function,
list::remove_if, exists, which allows for a condition other than a plain value comparison to be performed on each element in order to determine the elements to be removed.
Notice that a global algorithm function,
remove, exists with a similar behavior but operating between two iterators.
Parameters
- value
- Value of the elements to be removed.
T is the first class template parameter (the type of the elements stored in the list container).
Return value
none
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// remove from list
#include <iostream>
#include <list>
using namespace std;
int main ()
{
int myints[]= {17,89,7,14};
list<int> mylist (myints,myints+4);
mylist.remove(89);
cout << "mylist contains:";
for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
|
Output:
Complexity
Linear in
list::size (comparisons).
See also
- list::remove_if
- Remove elements fulfilling condition (public member function template)
- list::erase
- Erase elements (public member function)
- list::unique
- Remove duplicate values (public member function)
- list::pop_back
- Delete last element (public member function)
- list::pop_front
- Delete first element (public member function)