std::copy, std::copy_if

< cpp‎ | algorithm
Revision as of 18:17, 22 January 2013 by Nate (Talk | contribs)


Template:ddcl list begin <tr class="t-dsc-header">

Defined in header <algorithm>

<td></td> <td></td> <tr class="t-dcl ">

<td >
template< class InputIt, class OutputIt >
OutputIt copy( InputIt first, InputIt last, OutputIt d_first );

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class InputIt, class OutputIt, class UnaryPredicate >

OutputIt copy_if( InputIt first, InputIt last,
                  OutputIt d_first,

                  UnaryPredicate pred );

<td > (2) </td> <td > (since C++11) </td> </tr> Template:ddcl list end

Copies the elements in the range, defined by [first, last), to another range beginning at d_first. The second function only copies the elements for which the predicate pred returns true.



first, last - the range of elements to copy
d_first - the beginning of the destination range. If d_first is within [first, last), std::copy_backward must be used instead of std::copy.
pred - unary predicate which returns ​true for the required elements.

The signature of the predicate function should be equivalent to the following:

 bool pred(const Type &a);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The type Type must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type. ​

Type requirements
InputIt must meet the requirements of InputIterator.
OutputIt must meet the requirements of OutputIterator.
UnaryPredicate must meet the requirements of Predicate.

Return value

Output iterator to the element in the destination range, one past the last element copied.


1) Exactly last - first assignments

2) Exactly last - first applications of the predicate


In practice, implementations of std::copy avoid multiple assignments and use bulk copy functions such as std::memcpy if the value type is TriviallyCopyable

Possible implementation

First version
template<class InputIt, class OutputIt>
OutputIt copy(InputIt first, InputIt last, 
              OutputIt d_first)
    while (first != last) {
        *d_first++ = *first++;
    return d_first;
Second version
template<class InputIt, class OutputIt, class UnaryPredicate>
OutputIt copy_if(InputIt first, InputIt last, 
                 OutputIt d_first, UnaryPredicate pred)
    while (first != last) {
            *d_first++ = *first;
    return d_first;


The following code uses copy to both copy the contents of one vector to another and to display the resulting vector:

#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>
int main()
    std::vector<int> from_vector;
    for (int i = 0; i < 10; i++) {
    std::vector<int> to_vector(10);
    std::copy(from_vector.begin(), from_vector.end(), to_vector.begin());
    std::cout << "to_vector contains: ";
    std::copy(to_vector.begin(), to_vector.end(), 
              std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;


to_vector contains: 0 1 2 3 4 5 6 7 8 9

See also

Template:cpp/algorithm/dcl list copy backwardTemplate:cpp/algorithm/dcl list remove copy