remove_if


Remove items from a sequence that satisfy a predicate.

Remove all elements that satisfy pred from the sequence [ first ... last ). Return an iterator equal to first + n , where n equals the number of elements removed. The size of the container is not altered. If n elements are removed, the last n elements of the sequence [ first ... last ) have undefined values.

Library

Standards<ToolKit>

Declaration


#include <algorithm>

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

Complexity

Time complexity is linear, as ( last - first ) comparisons are performed. Space complexity is constant.

Example <ospace/osstd/examples/remif1.cpp>
#include <iostream>
#include <algorithm>

int numbers[ 6 ] = { 0, 0, 1, 1, 2, 2 };

bool
odd( int a_ )
  {
  return a_ % 2;
  }

void
main()
  {
  remove_if( numbers, numbers + 6, odd );

  for ( int i = 0; i < 6; ++i )
    cout << numbers[ i ] << ` `;
  cout << "\n";
  }

0 0 2 2 2 2

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.