std::partition_copy(3)
π
std::partition_copy(3) β C++ Standard Libary
okay so bestie, this is the std::partition_copy(3) page from the C++ Standard Libary manual, and it is literally the algorithm that copies your range into two different buckets based on a predicate. one bucket is for the yeses, one is for the nos. it's giving organized chaos, and we're obsessed π
πΈ NAME
std::partition_copy β yes bestie, that's the name, and it's also std::partition_copy. iconic behavior, no notes.
β¨ Synopsis
Defined in header <algorithm>:
template< class InputIt, class OutputIt1,
class OutputIt2, class UnaryPred >
std::pair<OutputIt1, OutputIt2>
partition_copy( InputIt first, InputIt last,
OutputIt1 d_first_true, OutputIt2 d_first_false,
UnaryPred p );
- Overload (1) β
since C++11;constexpr since C++20β¨
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class ForwardIt3, class UnaryPred >
std::pair<ForwardIt2, ForwardIt3>
partition_copy( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first_true, ForwardIt3 d_first_false,
UnaryPred p );
- Overload (2) β
since C++17π
- Copies the elements from the range
[first, last)into two different ranges, depending on what the predicatepreturns:- 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 at
d_first_false.
- The elements that satisfy the predicate
- Same as (1), but executed according to
policy.
This overload participates in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true (until C++20), or if std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true (since C++20).
If*firstis not writable tod_first_trueord_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. no cap, do not do this π
π Parameters
first,lastβ the range 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β the execution policy to use. See execution policy for details.pβ unary predicate which returnstrueif the element should be placed ind_first_true.
The expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of InputIt, regardless of value category, and must not modify v. Thus, a parameter type of VT& is not allowed, nor is VT unless for VT a move is equivalent to a copy (since C++11).
π§ Type requirements
InputItmust meet the requirements ofLegacyInputIterator.OutputIt1,OutputIt2must meet the requirements ofLegacyOutputIterator.ForwardIt1,ForwardIt2,ForwardIt3must meet the requirements ofLegacyForwardIterator.UnaryPredmust meet the requirements ofPredicate.
π 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. cute, tidy, done π
π₯ Complexity
Exactly std::distance(first, last) applications of p.
For the overload (2), 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
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)
template<class InputIt, class OutputIt1,
class OutputIt2, class UnaryPred>
constexpr //< since C++20
std::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;
}
}
return std::pair<OutputIt1, OutputIt2>(d_first_true, d_first_false);
}
π Example
// Run this code
#include <algorithm>
#include <iostream>
#include <utility>
void print(auto rem, const auto& v)
{
for (std::cout << rem; const auto& 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
π©Ή Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards:
- DR
P0896R4β applied toC++11/C++17.- Behavior as published: 1. the value type of
InputIt(C++11) /ForwardIt1(C++17) was required to beCopyAssignable; 2. the two output ranges could overlap. - Correct behavior: 1. not required; 2. the behavior is undefined in this case.
- Behavior as published: 1. the value type of
π See also
partitionβ divides a range of elements into two groups (function template).stable_partitionβ divides elements into two groups while preserving their relative order (function template).copyβ copies a range of elements to a new location (function template).copy_ifβ (function template) (C++11).remove_copy,remove_copy_ifβ copies a range of elements omitting those that satisfy specific criteria (function template).ranges::partition_copyβ copies a range dividing the elements into two groups (niebloid) (C++20).
mwah π β this has been the std::partition_copy(3) manual page, source/version http://cppreference.com and date 2024.06.10. bye bestie, go partition something cute π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms