SysTutorials

Linux Manual Pages › Section 3

stdcopystdcopy_if(3)

std::copy,std::copy_if - std::copy,std::copy_if

Synopsis

Defined in header <algorithm>
template< class InputIt, class OutputIt >                                                                   (until C++20)
OutputIt copy( InputIt first, InputIt last, OutputIt d_first );
template< class InputIt, class OutputIt >                                                                   (since C++20)
constexpr OutputIt copy( InputIt first, InputIt last, OutputIt d_first );
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >                                   (2) (since C++17)
ForwardIt2 copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first );
template< class InputIt, class OutputIt, class UnaryPredicate >
OutputIt copy_if( InputIt first, InputIt last,                                                                            (since C++11)
OutputIt d_first,                                                                                   (1)                   (until C++20)
UnaryPredicate pred );
template< class InputIt, class OutputIt, class UnaryPredicate >
constexpr OutputIt copy_if( InputIt first, InputIt last,                                                (3)               (since C++20)
OutputIt d_first,
UnaryPredicate pred );
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate >
ForwardIt2 copy_if( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,                            (4)           (since C++17)
ForwardIt2 d_first,
UnaryPredicate pred );

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) starting from first and proceeding to last - 1. The behavior is undefined if d_first is within the range [first, last). In this case, std::copy_backward may be used instead.
  2. Only copies the elements for which the predicate pred returns true. The relative order of the elements that are copied 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. This overload only participates in overload resolution if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true

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.               unary predicate which returns true for the required elements. pred - The expression pred(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of InputIt, regardless of value_category, and must not modify v. Thus, a parameter type of VT&is not allowed               , nor is VT unless for VT a move is equivalent to a copy               (since C++11).

Type requirements

InputIt must meet the requirements of LegacyInputIterator.

OutputIt must meet the requirements of LegacyOutputIterator.

ForwardIt1, ForwardIt2 must meet the requirements of LegacyForwardIterator.

UnaryPredicate must meet the requirements of Predicate.

Return value

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

Complexity

For the overloads with an ExecutionPolicy, there may be a performance cost if ForwardIt1’s value_type is not MoveConstructible.

Exceptions

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

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).

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;   }

Example

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

#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

See also

               copies a range of elements in backwards order copy_backward (function template)                creates a copy of a range that is reversed reverse_copy (function template) copy_n copies a number of elements to a new location                (function template) (C++11)                copy-assigns the given value to every element in a range fill (function template)                copies a range of elements omitting those that satisfy specific criteria remove_copy (function template) remove_copy_if