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, std::indirectly_unary_invocable<std::projected<I, Proj>> Fun> | (1) | (since C++20) |
template<ranges::input_range R,class Proj=std::identity, std::indirectly_unary_invocable< | (2) | (since C++20) |
Helper types | ||
template<class I,class F> using for_each_result=ranges::in_fun_result<I, F>; | (3) | (since C++20) |
[
first,
last)
, in order.For both overloads, if the iterator type is mutable,f may modify the elements of the range through the dereferenced iterator. Iff returns a result, the result is ignored.
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 apply the function to |
r | - | the range of elements to apply the function to |
f | - | the function to apply to the projected range |
proj | - | projection to apply to the elements |
{ranges::next(std::move(first), last), std::move(f)}
Exactlyranges::distance(first, last) applications off andproj.
struct for_each_fn{template<std::input_iterator I,std::sentinel_for<I> S,class Proj=std::identity,std::indirectly_unary_invocable<std::projected<I, Proj>> Fun>constexpr ranges::for_each_result<I, Fun> operator()(I first, S last, Fun f, Proj proj={})const{for(; first!= last;++first)std::invoke(f,std::invoke(proj,*first));return{std::move(first), std::move(f)};} template<ranges::input_range R,class Proj=std::identity,std::indirectly_unary_invocable<std::projected<ranges::iterator_t<R>, Proj>> Fun>constexpr ranges::for_each_result<ranges::borrowed_iterator_t<R>, Fun> operator()(R&& r, Fun f, Proj proj={})const{return(*this)(ranges::begin(r),ranges::end(r), std::move(f),std::ref(proj));}}; inlineconstexpr for_each_fn for_each; |
The following example uses alambda expression to increment all of the elements of a vector and then uses an overloadedoperator()
in a functor to compute their sum. Note that to compute the sum, it is recommended to use the dedicated algorithmstd::accumulate.
#include <algorithm>#include <cassert>#include <iostream>#include <string>#include <utility>#include <vector> struct Sum{void operator()(int n){ sum+= n;}int sum{0};}; int main(){std::vector<int> nums{3,4,2,8,15,267}; auto print=[](constauto& n){std::cout<<' '<< n;}; namespace ranges= std::ranges;std::cout<<"before:"; ranges::for_each(std::as_const(nums), print); print('\n'); ranges::for_each(nums,[](int& n){++n;}); // calls Sum::operator() for each numberauto[i, s]= ranges::for_each(nums.begin(), nums.end(), Sum());assert(i== nums.end()); std::cout<<"after: "; ranges::for_each(nums.cbegin(), nums.cend(), print); std::cout<<"\n""sum: "<< s.sum<<'\n'; using pair=std::pair<int,std::string>;std::vector<pair> pairs{{1,"one"},{2,"two"},{3,"tree"}}; std::cout<<"project the pair::first: "; ranges::for_each(pairs, print,[](const pair& p){return p.first;}); std::cout<<"\n""project the pair::second:"; ranges::for_each(pairs, print,&pair::second); print('\n');}
Output:
before: 3 4 2 8 15 267 after: 4 5 3 9 16 268sum: 305project the pair::first: 1 2 3project the pair::second: one two tree
range-for loop(C++11) | executes loop over range[edit] |
(C++20) | applies a function to a range of elements (algorithm function object)[edit] |
(C++20) | applies a function object to the first N elements of a sequence (algorithm function object)[edit] |
applies a unaryfunction object to elements from arange (function template)[edit] |