std::partition_copy() algorithm
- since C++20
- since C++17
// (1)
template< class InputIt, class OutputIt1,
class OutputIt2, class UnaryPredicate >
constexpr std::pair<OutputIt1, OutputIt2>
partition_copy( InputIt first, InputIt last,
OutputIt1 d_first_true, OutputIt2 d_first_false,
UnaryPredicate p );
// (2)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class ForwardIt3, class UnaryPredicate >
std::pair<ForwardIt2, ForwardIt3>
partition_copy( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first_true, ForwardIt3 d_first_false,
UnaryPredicate p );
// (1)
template< class InputIt, class OutputIt1,
class OutputIt2, class UnaryPredicate >
std::pair<OutputIt1, OutputIt2>
partition_copy( InputIt first, InputIt last,
OutputIt1 d_first_true, OutputIt2 d_first_false,
UnaryPredicate p );
-
(1) Copies the elements from the range [
first;last) to two different ranges depending on the value returned by the predicatep.The elements that satisfy the predicate
pare copied to the range beginning atd_first_true. The rest of the elements are copied to the range beginning atd_first_false.Undefined BehaviourThe behavior is undefined if the input range overlaps either of the output ranges.
-
(2) Same as (1), but executed according to
policy.Overload ResolutionThese overloads participate in overload resolution only if
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>(until C++20)std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>(since C++20) istrue.
Parameters
first last | The range of elements to copy from. |
d_first_true | The beginning of the output range for the elements that satisfy |
d_first_false | The beginning of the output range for the elements that don't satisfy |
policy | The execution policy to use. See execution policy for details. |
p | Unary predicate which returns The expression |
Type requirements
InputIt | LegacyInputIterator |
Dereferenced InputIt | CopyAssignable |
OutputIt1 OutputIt2 | LegacyOutputIterator |
ForwardIt1 ForwardIt2 ForwardIt3 | LegacyForwardIterator |
UnaryPredicate | Predicate |
ForwardIt1::value_type must be:
- CopyAssignable
- Writable to
ForwardIt2andForwardIt3 - Convertible to
UnaryPredicate's argument type
Return value
A std::pair constructed from the iterator to the end of the d_first_true range and the iterator to the end of the d_first_false range.
Complexity
Exactly std::distance(first, last) applications of p.
For the overload with an ExecutionPolicy, there may be a performance cost if ForwardIt's value type is not CopyConstructible.
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::terminateis called. For any otherExecutionPolicy, the behavior is implementation-defined. - If the algorithm fails to allocate memory,
std::bad_allocis thrown.
Possible implementation
partition_copy (1)
Examples
#include <algorithm>
#include <iostream>
#include <utility>
void print(auto rem, auto const& v)
{
for (std::cout << rem; auto const& 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) { return 4 < i; });
print("true_arr: ", true_arr);
print("false_arr: ", false_arr);
}
true_arr: 5 6 7 8 9
false_arr: 0 1 2 3 4
Hover to see the original license.