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 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
std::ranges
Non-modifying sequence operations | |||||||||||||||||||||||||||||||||
Modifying sequence operations | |||||||||||||||||||||||||||||||||
Partitioning operations | |||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||
Sorting operations | |||||||||||||||||||||||||||||||||
Binary search operations (on sorted ranges) | |||||||||||||||||||||||||||||||||
Set operations (on sorted ranges) | |||||||||||||||||||||||||||||||||
Heap operations | |||||||||||||||||||||||||||||||||
Minimum/maximum operations | |||||||||||||||||||||||||||||||||
Permutation operations | |||||||||||||||||||||||||||||||||
Fold operations | |||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||
Operations on uninitialized storage | |||||||||||||||||||||||||||||||||
Return types | |||||||||||||||||||||||||||||||||
Defined in header <algorithm> | ||
Call signature | ||
template<std::input_iterator I,std::sentinel_for<I> S, std::weakly_incrementable O1,std::weakly_incrementable O2, | (1) | (since C++20) |
template<ranges::input_range R, std::weakly_incrementable O1,std::weakly_incrementable O2, | (2) | (since C++20) |
Helper types | ||
template<class I,class O1,class O2> using partition_copy_result=ranges::in_out_out_result<I, O1, O2>; | (3) | (since C++20) |
[
first,
last)
to two different output ranges depending on the value returned by the predicatepred. The elements that satisfy the predicatepred after projection byproj are copied to the range beginning atout_true. The rest of the elements are copied to the range beginning atout_false. The behavior is undefined if the input range overlaps either of the output ranges.The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:
Contents |
first, last | - | the iterator-sentinel pair defining the sourcerange of elements to copy from |
r | - | the source range of elements to copy from |
out_true | - | the beginning of the output range for the elements that satisfypred |
out_false | - | the beginning of the output range for the elements that do not satisfypred |
pred | - | predicate to apply to the projected elements |
proj | - | projection to apply to the elements |
{last, o1, o2}, whereo1
ando2
are the ends of the output ranges respectively, after the copying is complete.
Exactlyranges::distance(first, last) applications of the corresponding predicatecomp and any projectionproj.
struct partition_copy_fn{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> requiresstd::indirectly_copyable<I, O1>&&std::indirectly_copyable<I, O2>constexpr ranges::partition_copy_result<I, O1, O2> operator()(I first, S last, O1 out_true, O2 out_false, Pred pred, Proj proj={})const{for(; first!= last;++first)if(!!std::invoke(pred,std::invoke(proj,*first)))*out_true=*first,++out_true;else*out_false=*first,++out_false;return{std::move(first), std::move(out_true), std::move(out_false)};} 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> requiresstd::indirectly_copyable<ranges::iterator_t<R>, O1>&&std::indirectly_copyable<ranges::iterator_t<R>, O2>constexpr ranges::partition_copy_result<ranges::borrowed_iterator_t<R>, O1, O2> operator()(R&& r, O1 out_true, O2 out_false, Pred pred, Proj proj={})const{return(*this)(ranges::begin(r),ranges::end(r), std::move(out_true), std::move(out_false), std::move(pred), std::move(proj));}}; inlineconstexpr partition_copy_fn partition_copy{}; |
#include <algorithm>#include <cctype>#include <iostream>#include <iterator>#include <vector> int main(){constauto 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){returnstd::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';}
Output:
in = N 3 U M 1 B 4 E 1 5 R 9o1 = N U M B E Ro2 = 3 1 4 1 5 9
(C++20) | divides a range of elements into two groups (algorithm function object)[edit] |
(C++20) | divides elements into two groups while preserving their relative order (algorithm function object)[edit] |
(C++20)(C++20) | copies a range of elements to a new location (algorithm function object)[edit] |
(C++20)(C++20) | copies a range of elements omitting those that satisfy specific criteria (algorithm function object)[edit] |
(C++11) | copies a range dividing the elements into two groups (function template)[edit] |