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 OutputIt1, class OutputIt2,class UnaryPred> | (1) | (since C++11) (constexpr since C++20) |
template<class ExecutionPolicy,class ForwardIt1,class ForwardIt2, class ForwardIt3,class UnaryPred> | (2) | (since C++17) |
[
first,
last)
to two different ranges depending on the value returned by the predicatep.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) |
If*first is notwritable tod_first_true ord_first_false, the program is ill-formed.
Among the input range and the two output ranges, if any two ranges overlap, the behavior is undefined.
Contents |
first, last | - | the pair of iterators defining the sourcerange of elements to copy from |
d_first_true | - | the beginning of the output range for the elements that satisfyp |
d_first_false | - | the beginning of the output range for the elements that do not satisfyp |
policy | - | theexecution policy to use |
p | - | unary predicate which returns true if the element should be placed in d_first_true. The expressionp(v) must be convertible tobool for every argument |
Type requirements | ||
-InputIt must meet the requirements ofLegacyInputIterator. | ||
-OutputIt1, OutputIt2 must meet the requirements ofLegacyOutputIterator. | ||
-ForwardIt1, ForwardIt2, ForwardIt3 must meet the requirements ofLegacyForwardIterator. | ||
-UnaryPred must meet the requirements ofPredicate. |
Astd::pair constructed from the iterator to the end of thed_first_true range and the iterator to the end of thed_first_false range.
Exactlystd::distance(first, last) applications ofp.
For the overload(2), there may be a performance cost ifForwardIt
's value type is notCopyConstructible.
The overload with a template parameter namedExecutionPolicy
reports errors as follows:
ExecutionPolicy
is one of thestandard policies,std::terminate is called. For any otherExecutionPolicy
, the behavior is implementation-defined.partition_copy (1) |
---|
template<class InputIt,class OutputIt1,class OutputIt2,class UnaryPred>constexpr//< since C++20std::pair<OutputIt1, OutputIt2> partition_copy(InputIt first, InputIt last, OutputIt1 d_first_true, OutputIt2 d_first_false, UnaryPred p){for(; first!= last;++first){if(p(*first)){*d_first_true=*first;++d_first_true;}else{*d_first_false=*first;++d_first_false;}} returnstd::pair<OutputIt1, OutputIt2>(d_first_true, d_first_false);} |
#include <algorithm>#include <iostream>#include <utility> void print(auto rem,constauto& v){for(std::cout<< rem;constauto& x: v)std::cout<< x<<' ';std::cout<<'\n';} int main(){int arr[10]={0,1,2,3,4,5,6,7,8,9};int true_arr[5]={0};int false_arr[5]={0}; std::partition_copy(std::begin(arr),std::end(arr),std::begin(true_arr),std::begin(false_arr),[](int i){return4< i;}); print("true_arr: ", true_arr); print("false_arr: ", false_arr);}
Output:
true_arr: 5 6 7 8 9false_arr: 0 1 2 3 4
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
P0896R4 | C++11 C++17 | 1. the value type ofInputIt (C++11)/ForwardIt1 (C++17)was required to beCopyAssignable 2. the two output ranges could overlap | 1. not required 2. the behavior is undefined in this case |
divides a range of elements into two groups (function template)[edit] | |
divides elements into two groups while preserving their relative order (function template)[edit] | |
(C++11) | copies a range of elements to a new location (function template)[edit] |
copies a range of elements omitting those that satisfy specific criteria (function template)[edit] | |
(C++20) | copies a range dividing the elements into two groups (algorithm function object)[edit] |