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, class Proj=std::identity, | (1) | (since C++20) |
template<ranges::input_range R,class Proj=std::identity, std::indirect_unary_predicate< | (2) | (since C++20) |
[
first,
last)
that satisfy the predicatepred after projection appear before all elements that don't. Also returnstrue if[
first,
last)
is empty.The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:
Contents |
first, last | - | the iterator-sentinel pair defining therange of elements to examine |
r | - | the range of elements to examine |
pred | - | predicate to apply to the projected elements |
proj | - | projection to apply to the elements |
true if the range[
first,
last)
is empty or is partitioned bypred,false otherwise.
At mostranges::distance(first, last) applications ofpred andproj.
struct is_partitioned_fn{template<std::input_iterator I,std::sentinel_for<I> S,class Proj=std::identity,std::indirect_unary_predicate<std::projected<I, Proj>> Pred>constexprbool operator()(I first, S last, Pred pred, Proj proj={})const{for(; first!= last;++first)if(!std::invoke(pred,std::invoke(proj,*first)))break; for(; first!= last;++first)if(std::invoke(pred,std::invoke(proj,*first)))returnfalse; returntrue;} template<ranges::input_range R,class Proj=std::identity,std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>> Pred>constexprbool operator()(R&& r, Pred pred, Proj proj={})const{return(*this)(ranges::begin(r),ranges::end(r),std::ref(pred),std::ref(proj));}}; inlineconstexprauto is_partitioned= is_partitioned_fn(); |
#include <algorithm>#include <array>#include <iostream>#include <numeric>#include <utility> int main(){std::array<int,9> v; auto print=[&v](bool o){for(int x: v)std::cout<< x<<' ';std::cout<<(o?"=> ":"=> not ")<<"partitioned\n";}; auto is_even=[](int i){return i%2==0;}; std::iota(v.begin(), v.end(),1);// or std::ranges::iota(v, 1); print(std::ranges::is_partitioned(v, is_even)); std::ranges::partition(v, is_even); print(std::ranges::is_partitioned(std::as_const(v), is_even)); std::ranges::reverse(v); print(std::ranges::is_partitioned(v.cbegin(), v.cend(), is_even)); print(std::ranges::is_partitioned(v.crbegin(), v.crend(), is_even));}
Output:
1 2 3 4 5 6 7 8 9 => not partitioned2 4 6 8 5 3 7 1 9 => partitioned9 1 7 3 5 8 6 4 2 => not partitioned9 1 7 3 5 8 6 4 2 => partitioned
(C++20) | divides a range of elements into two groups (algorithm function object)[edit] |
(C++20) | locates the partition point of a partitioned range (algorithm function object)[edit] |
(C++11) | determines if the range is partitioned by the given predicate (function template)[edit] |