On this page
std::partition_copy
Defined in header <algorithm> |
||
---|---|---|
(1) | ||
|
(since C++11) (until C++20) |
|
|
(since C++20) | |
|
(2) | (since C++17) |
[
first
,
last
)
to two different ranges depending on the value returned by the predicate p
. The elements that satisfy the predicate p
are copied to the range beginning at d_first_true
. The rest of the elements are copied to the range beginning at d_first_false
.
policy
. This overload does not participate in overload resolution unless
|
(until C++20) |
|
(since C++20) |
Parameters
first, last | - | the range of elements to copy from |
d_first_true | - | the beginning of the output range for the elements that satisfy p |
d_first_false | - | the beginning of the output range for the elements that do not satisfy p |
policy | - | the execution policy to use. See execution policy for details. |
p | - | unary predicate which returns true if the element should be placed in d_first_true. The expression |
Type requirements | ||
-InputIt must meet the requirements of LegacyInputIterator. |
||
-The type of dereferenced InputIt must meet the requirements of CopyAssignable. |
||
-OutputIt1, OutputIt2 must meet the requirements of LegacyOutputIterator. |
||
-ForwardIt1, ForwardIt2, ForwardIt3 must meet the requirements of LegacyForwardIterator. ForwardIt1's value type must be CopyAssignable, writable to ForwardIt2 and ForwardIt3, and convertible to UnaryPredicate's argument type. |
||
-UnaryPredicate must meet the requirements of Predicate. |
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 overload with a template parameter named ExecutionPolicy
reports errors as follows:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicy
is 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.
Possible implementation
|
Example
#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);
}
Output:
true_arr: 5 6 7 8 9
false_arr: 0 1 2 3 4
See also
divides a range of elements into two groups (function template) |
|
divides elements into two groups while preserving their relative order (function template) |
|
(C++11)
|
copies a range of elements to a new location (function template) |
copies a range of elements omitting those that satisfy specific criteria (function template) |
|
(C++20)
|
copies a range dividing the elements into two groups (niebloid) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/partition_copy