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_if

public member function template
template <class Predicate>
  void remove_if ( Predicate pred );

Remove elements fulfilling condition

Removes from the list all the elements for which Predicate pred returns true. This calls the destructor of these objects and reduces the list size by the amount of elements removed.

Predicate pred can be implemented as any typed expression taking one argument of the same type as the list and returning a bool (this may either be a function pointer or an object whose class implements operator().

The function calls pred(*i) for each element (where i is an iterator to that element). Any of the elements in the list for which this returns true, is removed from the container.

Notice that a global algorithm function, remove_if, exists with a similar behavior but operating between two iterators.

Parameters

pred
Unary predicate that, taking a value of the same type as those contained in the list object, returns true for those values to be removed from the container, and false for those remaining.

Return value

none

Example

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
// list::remove_if
#include <iostream>
#include <list>
using namespace std;
// a predicate implemented as a function:
bool single_digit (const int& value) { return (value<10); }
// a predicate implemented as a class:
class is_odd
{
public:
  bool operator() (const int& value) {return (value%2)==1; }
};
int main ()
{
  int myints[]= {15,36,7,17,20,39,4,1};
  list<int> mylist (myints,myints+8);   // 15 36 7 17 20 39 4 1
  mylist.remove_if (single_digit);      // 15 36 17 20 39
  mylist.remove_if (is_odd());          // 36 20
  cout << "mylist contains:";
  for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    cout << " " << *it;
  cout << endl;
  return 0;
}


Output:
mylist contains: 36 20

Complexity

Linear in list::size (predicates).

See also