Top of document
©Copyright 1999 Rogue Wave Software

remove_if


     Algorithm

Summary

Move desired elements to the front of a container, and return an iterator that describes where the sequence of desired elements ends.

Contents

Synopsis

#include <algorithm>
template <class ForwardIterator, class Predicate>
ForwardIterator remove_if (ForwardIterator first,
                           ForwardIterator last,
                           Predicate pred);

Description

The remove_if algorithm eliminates all the elements referred to by iterator i in the range [first, last) for which the following corresponding condition holds: pred(*i) == true. remove_if returns the end of the resulting range. remove_if is stable, that is, the relative order of the elements that are not removed is the same as their relative order in the original range.

remove_if does not actually reduce the size of the sequence. It actually operates by: 1) copying the values that are to be retained to the front of the sequence, and 2) returning an iterator that describes where the sequence of retained values ends. Elements that are after this iterator are simply the original sequence values, left unchanged. Here's a simple example:

Say we want to remove all even numbers from the following sequence:

          123456789

Applying the remove_if algorithm results in the following sequence:

          13579|XXXX

The vertical bar represents the position of the iterator returned by remove_if. Note that the elements to the left of the vertical bar are the original sequence with the even numbers removed. The elements to the right of the bar are simply the untouched original members of the original sequence.

If you want to actually delete items from the container, use the following technique:

container.erase(remove(first,last,value),container.end());

Complexity

Exactly last1 - first1 applications of the corresponding predicate are done.

Example

//
// remove.cpp
//
 #include <algorithm>
 #include <vector>
 #include <iterator>
 #include <iostream.h> 
 template<class Arg>
 struct all_true : public unary_function<Arg, bool>
 {
   bool operator()(const Arg& x){ return 1; }
 };
 int main ()
 {
   int arr[10] = {1,2,3,4,5,6,7,8,9,10};
   vector<int> v(arr, arr+10);
   copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
   cout << endl << endl;
   // remove the 7
   vector<int>::iterator result = 
           remove(v.begin(), v.end(), 7);
   // delete dangling elements from the vector
   v.erase(result, v.end());
   copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
   cout << endl << endl;
   // remove everything beyond the fourth element
   result = remove_if(v.begin()+4, 
                      v.begin()+8, all_true<int>());
   // delete dangling elements
   v.erase(result, v.end());
   copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
   cout << endl << endl;
   return 0;
 }
Output :
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 8 9 10
1 2 3 4
1 2 4

Warning

If your compiler does not support default template parameters, then you need to always supply the Allocator template argument. For instance, you will need to write :

vector<int, allocator>

instead of :

vector<int>

See Also

remove, remove_copy, remove_copy_if


Top of document