In a range of elements, copies the elements that are true for the specified condition.

template<class InputIterator, class OutputIterator, class BinaryPredicate>
   OutputIterator copy_if(
      InputIterator _First, 
      InputIterator _Last,
      OutputIterator _Dest,
      Predicate _Pred


  • _First
    An input iterator that indicates the start of a range to check for the condition.

  • _Last
    An input iterator that indicates the end of the range.

  • _Dest
    The output iterator that indicates the destination for the copied elements.

  • _Pred
    The condition against which every element in the range is tested. This condition is provided by a user-defined predicate function object. A predicate takes one argument and returns true or false.

Return Value

An output iterator that equals _Dest incremented once for each element that fulfills the condition. In other words, the return value minus _Dest equals the number of copied elements.


The template function evaluates

if (_Pred(*_First + N))

*_Dest++ = *(_First + N))

once for each N in the range [0, _Last - _First), for strictly increasing values of N starting with the lowest value. If _Dest and _First designate regions of storage, _Dest must not be in the range [_First, _Last).


Header: <algorithm>

Namespace: std

See Also


Standard Template Library

Other Resources

<algorithm> Members

Change History




April 2011

Fixed errors in the syntax.

Customer feedback.