| Constrained algorithms and algorithms on ranges(C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Constrained algorithms, e.g.ranges::copy,ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Execution policies(C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized memory | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 atd_first (copy destination range).
[first, last) starting fromfirst and proceeding tolast.[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>> istrue. | (until C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> istrue. | (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>> istrue. | (until C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> istrue. | (since C++20) |
Contents |
| first, last | - | the pair of iterators defining the sourcerange of elements to copy |
| d_first | - | the beginning of the destination range |
| policy | - | theexecution policy to use |
| pred | - | unary predicate which returns true for the required elements. The expressionpred(v) must be convertible tobool for every argument |
| Type requirements | ||
-InputIt must meet the requirements ofLegacyInputIterator. | ||
-OutputIt must meet the requirements ofLegacyOutputIterator. | ||
-ForwardIt1, ForwardIt2 must meet the requirements ofLegacyForwardIterator. | ||
-UnaryPred must meet the requirements ofPredicate. | ||
Output iterator to the element in the destination range, one past the last element copied.
Given\(\scriptsize N\)N asstd::distance(first, last):
For the overloads with anExecutionPolicy, there may be a performance cost ifForwardIt1's value type is notMoveConstructible.
The overloads with a template parameter namedExecutionPolicy report errors as follows:
ExecutionPolicy is one of thestandard policies,std::terminate is called. For any otherExecutionPolicy, the behavior is implementation-defined.| 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;} |
In practice, implementations ofstd::copy avoid multiple assignments and use bulk copy functions such asstd::memmove if the value type isTriviallyCopyable and the iterator types satisfyLegacyContiguousIterator.
When copying overlapping ranges,std::copy is appropriate when copying to the left (beginning of the destination range is outside the source range) whilestd::copy_backward is appropriate when copying to the right (end of the destination range is outside the source range).
The following code usesstd::copy both to copy the contents of onestd::vector to another and to display the resultingstd::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>(std::cout," "));std::cout<<'\n'; std::cout<<"odd numbers in to_vector are: "; std::copy_if(to_vector.begin(), to_vector.end(),std::ostream_iterator<int>(std::cout," "),[](int x){return x%2!=0;});std::cout<<'\n'; std::cout<<"to_vector contains these multiples of 3: "; to_vector.clear(); std::copy_if(from_vector.begin(), from_vector.end(),std::back_inserter(to_vector),[](int x){return x%3==0;}); for(constint x: to_vector)std::cout<< x<<' ';std::cout<<'\n';}
Possible output:
to_vector contains: 0 1 2 3 4 5 6 7 8 9odd numbers in to_vector are: 1 3 5 7 9to_vector contains these multiples of 3: 0 3 6 9
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2039 | C++11 | the return value ofstd::copy_if was not specified | specified |
| LWG 2044 | C++11 | the stability ofstd::copy_if was not defined | defined |
| 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] |
| copy-assigns the given value to every element in a range (function template)[edit] | |
| copies a range of elements omitting those that satisfy specific criteria (function template)[edit] | |
(C++20)(C++20) | copies a range of elements to a new location (algorithm function object)[edit] |