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::forward_iterator I,std::sentinel_for<I> S, class Proj=std::identity, | (1) | (since C++20) |
template<ranges::forward_range R, class Proj=std::identity, | (2) | (since C++20) |
Examines the partitioned (as if byranges::partition) range[
first,
last)
orr and locates the end of the first partition, that is, the projected element that does not satisfypred orlast if all projected elements satisfypred.
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 partially-orderedrange of elements to examine |
r | - | the partially-ordered range to examine |
pred | - | predicate to apply to the projected elements |
proj | - | projection to apply to the elements |
The iterator past the end of the first partition within[
first,
last)
or the iterator equal tolast if all projected elements satisfypred.
GivenN=ranges::distance(first, last), performsO(log N) applications of the predicatepred and projectionproj.
However, if sentinels don't modelstd::sized_sentinel_for<I>, the number of iterator increments isO(N).
This algorithm is a more general form ofranges::lower_bound
, which can be expressed in terms ofranges::partition_point
with the predicate[&](autoconst& e){returnstd::invoke(pred, e, value);});.
#include <algorithm>#include <array>#include <iostream>#include <iterator> auto print_seq=[](auto rem,auto first,auto last){for(std::cout<< rem; first!= last;std::cout<<*first++<<' '){}std::cout<<'\n';}; int main(){std::array v{1,2,3,4,5,6,7,8,9}; auto is_even=[](int i){return i%2==0;}; std::ranges::partition(v, is_even); print_seq("After partitioning, v: ", v.cbegin(), v.cend()); constauto pp= std::ranges::partition_point(v, is_even);constauto i= std::ranges::distance(v.cbegin(), pp);std::cout<<"Partition point is at "<< i<<"; v["<< i<<"] = "<<*pp<<'\n'; print_seq("First partition (all even elements): ", v.cbegin(), pp); print_seq("Second partition (all odd elements): ", pp, v.cend());}
Possible output:
After partitioning, v: 2 4 6 8 5 3 7 1 9Partition point is at 4; v[4] = 5First partition (all even elements): 2 4 6 8Second partition (all odd elements): 5 3 7 1 9
(C++20) | checks whether a range is sorted into ascending order (algorithm function object)[edit] |
(C++20) | returns an iterator to the first elementnot less than the given value (algorithm function object)[edit] |
(C++11) | locates the partition point of a partitioned range (function template)[edit] |