Namespaces
Variants
Views
Actions

std::copy, std::copy_if

From cppreference.com
< cpp‎ | algorithm
 
 
Algorithm library
Execution policies (C++17)
Non-modifying sequence operations
(C++11)(C++11)(C++11)
(C++17)
Modifying sequence operations
copycopy_if
(C++11)
(C++11)
(C++11)

Operations on uninitialized storage
Partitioning operations
Sorting operations
(C++11)
Binary search operations
Set operations (on sorted ranges)
Heap operations
(C++11)
Minimum/maximum operations
(C++11)
(C++17)

Permutations
Numeric operations
C library
 
Defined in header <algorithm>
template< class InputIt, class OutputIt >
OutputIt copy( InputIt first, InputIt last, OutputIt d_first );
(1)
template< class ExecutionPolicy, class InputIt, class OutputIt >
OutputIt copy( ExecutionPolicy&& policy, InputIt first, InputIt last, OutputIt d_first );
(2) (since C++17)
template< class InputIt, class OutputIt, class UnaryPredicate >

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

                  UnaryPredicate pred );
(3) (since C++11)
template< class ExecutionPolicy, class InputIt, class OutputIt, class UnaryPredicate >

OutputIt copy_if( ExecutionPolicy&& policy, InputIt first, InputIt last,
                  OutputIt d_first,

                  UnaryPredicate pred );
(4) (since C++17)

Copies the elements in the range, defined by [first, last), to another range beginning at d_first.

1) Copies all elements in the range [first, last). The behavior is undefined if d_first is within the range [first, last). In this case, std::copy_backward may be used instead.
3) Only copies the elements for which the predicate pred returns true. The order of the elements that are not removed is preserved. The behavior is undefined if the source and the destination ranges overlap.
2,4) Same as (1,3), but executed according to policy. These overloads do not participate in overload resolution unless std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true

Contents

[edit] Parameters

first, last - the range of elements to copy
d_first - the beginning of the destination range.
policy - the execution policy to use. See execution policy for details.
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.

[edit] Return value

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

[edit] Complexity

1-2) Exactly last - first assignments

3-4) Exactly last - first applications of the predicate

[edit] Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the three standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

[edit] Notes

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

When copying overlapping ranges, std::copy is appropriate when copying to the left (beginning of the destination range is outside the source range) while std::copy_backward is appropriate when copying to the right (end of the destination range is outside the source range).

[edit] 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) {
        if (pred(*first))
            *d_first++ = *first;
        first++;
    }
    return d_first;
}

[edit] Example

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>
#include <numeric>
 
int main()
{
    std::vector<int> from_vector(10);
    std::iota(from_vector.begin(), from_vector.end(), 0);
 
    std::vector<int> to_vector;
    std::copy(from_vector.begin(), from_vector.end(),
              std::back_inserter(to_vector));
// or, alternatively,
//  std::vector<int> to_vector(from_vector.size());
//  std::copy(from_vector.begin(), from_vector.end(), to_vector.begin());
// either way is equivalent to
//  std::vector<int> to_vector = from_vector;
 
    std::cout << "to_vector contains: ";
 
    std::copy(to_vector.begin(), to_vector.end(),
              std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
}

Output:

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

[edit] See also

copies a range of elements in backwards order
(function template) [edit]
creates a copy of a range that is reversed
(function template) [edit]
(C++11)
copies a number of elements to a new location
(function template) [edit]
assigns a range of elements a certain value
(function template) [edit]
copies a range of elements omitting those that satisfy specific criteria
(function template) [edit]
parallelized version of std::copy
(function template) [edit]
parallelized version of std::copy_if
(function template) [edit]