cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : list : remove
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
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

// 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

list::remove_if Remove elements fulfilling condition (public member function template)
list::erase Erase elements (public member function)
list::unique Remove duplicate values (member function)
list::pop_back Delete last element (public member function)
list::pop_front Delete first element (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us