std::copy, std::copy_if
| Defined in header <algorithm>
|
||
template< class InputIt, class OutputIt > OutputIt copy( InputIt first, InputIt last, |
(1) | (constexpr since C++20) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > |
(2) | (since C++17) |
template< class InputIt, class OutputIt, class UnaryPred > OutputIt copy_if( InputIt first, InputIt last, |
(3) | (since C++11) (constexpr since C++20) |
| template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPred > |
(4) | (since C++17) |
Copies the elements in the range, defined by [first, last), to another range beginning at d_first (copy destination range).
[first, last) starting from first and proceeding to last.[first, last), the behavior is undefined. In this case, std::copy_backward may be used instead.|
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true. |
(until C++20) |
|
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true. |
(since C++20) |
[first, last) and the copy destination range overlaps, the behavior is undefined.[first, last) and the copy destination range overlaps, the behavior is undefined.|
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true. |
(until C++20) |
|
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true. |
(since C++20) |
Contents |
[edit] Parameters
| first, last | - | the pair of iterators defining the source range of elements to copy |
| d_first | - | the beginning of the destination range |
| policy | - | the execution policy to use |
| pred | - | unary predicate which returns true for the required elements. The expression pred(v) must be convertible to bool for every argument |
| Type requirements | ||
-InputIt must meet the requirements of LegacyInputIterator.
| ||
-OutputIt must meet the requirements of LegacyOutputIterator.
| ||
-ForwardIt1, ForwardIt2 must meet the requirements of LegacyForwardIterator.
| ||
-UnaryPred 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
Given N as std::distance(first, last):
For the overloads with an ExecutionPolicy, there may be a performance cost if ForwardIt1's value type is not MoveConstructible.
[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
ExecutionPolicyis one of the standard policies, std::terminate is called. For any otherExecutionPolicy, the behavior is implementation-defined. - If the algorithm fails to allocate memory, std::bad_alloc is thrown.
[edit] Possible implementation
| copy (1) |
|---|
template<class InputIt, class OutputIt> OutputIt copy(InputIt first, InputIt last, OutputIt d_first) { for (; first != last; (void)++first, (void)++d_first) *d_first = *first; return d_first; } |
| copy_if (3) |
template<class InputIt, class OutputIt, class UnaryPred> OutputIt copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPred pred) { for (; first != last; ++first) if (pred(*first)) { *d_first = *first; ++d_first; } return d_first; } |
[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 and the iterator types satisfy LegacyContiguousIterator.
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] Example
The following code uses std::copy both to copy the contents of one std::vector to another and to display the resulting std::vector.
#include <algorithm> #include <iostream> #include <iterator> #include <numeric> #include <vector> 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>(