std::ranges::partition_copy() algorithm
- od C++20
- Simplified
- Detailed
// (1)
constexpr partition_copy_result<I, O1, O2>
partition_copy( I first, S last, O1 out_true, O2 out_false,
Pred pred, Proj proj = {} );
// (2)
constexpr partition_copy_result<ranges::borrowed_iterator_t<R>, O1, O2>
partition_copy( R&& r, O1 out_true, O2 out_false,
Pred pred, Proj proj = {} );
The type of arguments are generic and have the following constraints:
I-std::input_iteratorS-std::sentinel_for<I>O1,O2-std::weakly_incrementableR-std::ranges::forward_rangePred:- (1) -
std::indirect_unary_predicate<std::projected<I, Proj>> - (2) -
std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>
- (1) -
Proj- (none)
The Proj template argument has the following default type std::identity for all overloads.
Additionally, each overload has the following constraints:
- (1) -
indirectly_copyable<I, O1> && indirectly_copyable<I, O2> - (2) -
indirectly_copyable<ranges::iterator_t<R>, O1> && indirectly_copyable<ranges::iterator_t<R>, O2>
(The std:: namespace was omitted here for readability)
// (1)
template<
std::input_iterator I,
std::sentinel_for<I> S,
std::weakly_incrementable O1,
std::weakly_incrementable O2,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred
>
requires std::indirectly_copyable<I, O1>
&& std::indirectly_copyable<I, O2>
constexpr partition_copy_result<I, O1, O2>
partition_copy( I first, S last, O1 out_true, O2 out_false,
Pred pred, Proj proj = {} );
// (2)
template<
ranges::input_range R,
std::weakly_incrementable O1,
std::weakly_incrementable O2,
class Proj = std::identity,
std::indirect_unary_predicate<std::projected<iterator_t<R>, Proj>> Pred
>
requires std::indirectly_copyable<ranges::iterator_t<R>, O1>
&& std::indirectly_copyable<ranges::iterator_t<R>, O2>
constexpr partition_copy_result<ranges::borrowed_iterator_t<R>, O1, O2>
partition_copy( R&& r, O1 out_true, O2 out_false,
Pred pred, Proj proj = {} );
-
(1) Copies the elements from the input range [
first;last) to two different output ranges depending on the value returned by the predicatepred.The elements that satisfy the predicate
predafter projection byprojare copied to the range beginning atout_true.
The rest of the elements are copied to the range beginning atout_false.Undefined BehaviourThe behavior is undefined if the input range overlaps either of the output ranges.
-
(2) Same as (1), but uses
ras the range, as if usingranges::begin(r)asfirstandranges::end(r)aslast.
The function-like entities described on this page are niebloids.
Parameters
first last | The input range of elements to copy from. |
r | The input range of elements to copy from. |
out_true | The beginning of the output range for the elements that satisfy |
out_false | The beginning of the output range for the elements that don't satisfy |
pred | The predicate to apply to the projected elements. |
proj | The projection to apply to the elements. |
Return value
{
last,
o1,
o2
}
Where o1 and o2 are the ends of the output ranges respectively, after the copying is complete.
Complexity
Exactly ranges::distance(first, last) applications of the corresponding predicate pred and any projection proj.
Exceptions
(none)
Possible implementation
partition_copy(1) and partition_copy(2)
Examples
#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
const auto in = {'N', '3', 'U', 'M', '1', 'B', '4', 'E', '1', '5', 'R', '9'};
std::vector<int> o1(size(in)), o2(size(in));
auto pred = [](char c) { return std::isalpha(c); };
auto ret = std::ranges::partition_copy(in, o1.begin(), o2.begin(), pred);
std::ostream_iterator<char> cout {std::cout, " "};
std::cout << "in = ";
std::ranges::copy(in, cout);
std::cout << "\no1 = ";
std::copy(o1.begin(), ret.out1, cout);
std::cout << "\no2 = ";
std::copy(o2.begin(), ret.out2, cout);
std::cout << '\n';
}
in = N 3 U M 1 B 4 E 1 5 R 9
o1 = N U M B E R
o2 = 3 1 4 1 5 9
Hover to see the original license.