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, std::indirect_binary_predicate< | (1) | (since C++20) |
template<ranges::forward_range R,class Proj=std::identity, std::indirect_binary_predicate< | (2) | (since C++20) |
Searches the range[
first,
last)
for the first two consecutive equal elements.
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 the elements to examine |
pred | - | predicate to apply to the projected elements |
proj | - | projection to apply to the elements |
An iterator to the first of the first pair of identical elements, that is, the first iteratorit
such thatbool(std::invoke(pred,std::invoke(proj1,*it),std::invoke(proj,*(it+1)))) istrue.
If no such elements are found, an iterator equal tolast is returned.
Exactlymin((result- first)+1,(last- first)-1) applications of the predicate and projection whereresult
is the return value.
struct adjacent_find_fn{template<std::forward_iterator I,std::sentinel_for<I> S,class Proj=std::identity,std::indirect_binary_predicate< std::projected<I, Proj>, std::projected<I, Proj>> Pred=ranges::equal_to>constexpr I operator()(I first, S last, Pred pred={}, Proj proj={})const{if(first== last)return first;auto next=ranges::next(first);for(; next!= last;++next,++first)if(std::invoke(pred,std::invoke(proj,*first),std::invoke(proj,*next)))return first;return next;} template<ranges::forward_range R,class Proj=std::identity,std::indirect_binary_predicate< std::projected<ranges::iterator_t<R>, Proj>, std::projected<ranges::iterator_t<R>, Proj>> Pred=ranges::equal_to>constexprranges::borrowed_iterator_t<R> operator()(R&& r, Pred pred={}, Proj proj={})const{return(*this)(ranges::begin(r),ranges::end(r),std::ref(pred),std::ref(proj));}}; inlineconstexpr adjacent_find_fn adjacent_find; |
#include <algorithm>#include <functional>#include <iostream>#include <ranges> constexprbool some_of(auto&& r,auto&& pred)// some but not all{return std::ranges::cend(r)!= std::ranges::adjacent_find(r,[&pred](autoconst& x,autoconst& y){return pred(x)!= pred(y);});} // test some_ofconstexprauto a={0,0,0,0}, b={1,1,1,0}, c={1,1,1,1};auto is_one=[](auto x){return x==1;};static_assert(!some_of(a, is_one)&& some_of(b, is_one)&&!some_of(c, is_one)); int main(){constauto v={0,1,2,3,40,40,41,41,5};/* ^^ ^^ */namespace ranges= std::ranges; if(auto it= ranges::adjacent_find(v.begin(), v.end()); it== v.end())std::cout<<"No matching adjacent elements\n";elsestd::cout<<"The first adjacent pair of equal elements is at ["<<ranges::distance(v.begin(), it)<<"] == "<<*it<<'\n'; if(auto it= ranges::adjacent_find(v,ranges::greater()); it== v.end())std::cout<<"The entire vector is sorted in ascending order\n";elsestd::cout<<"The last element in the non-decreasing subsequence is at ["<<ranges::distance(v.begin(), it)<<"] == "<<*it<<'\n';}
Output:
The first adjacent pair of equal elements is at [4] == 40The last element in the non-decreasing subsequence is at [7] == 41
(C++20) | removes consecutive duplicate elements in a range (algorithm function object)[edit] |
finds the first two adjacent items that are equal (or satisfy a given predicate) (function template)[edit] |