Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
list
comparison operators
list::list
list::~list
member functions:
list::assign
list::back
list::begin
list::clear
list::empty
list::end
list::erase
list::front
list::get_allocator
list::insert
list::max_size
list::merge
list::operator=
list::pop_back
list::pop_front
list::push_back
list::push_front
list::rbegin
list::remove
list::remove_if
list::rend
list::resize
list::reverse
list::size
list::sort
list::splice
list::swap
list::unique


list::remove

public member function
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:
mylist contains: 17 7 14

Complexity

Linear in list::size (comparisons).

See also