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,class Proj=std::identity, std::indirectly_unary_invocable<std::projected<I, Proj>> Fun> | (1) | (since C++20) |
Helper types | ||
template<class I,class F> using for_each_n_result=ranges::in_fun_result<I, F>; | (2) | (since C++20) |
[
first,
first+ n)
, in order.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. Ifn is less than zero, the behavior is undefined.
The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:
Contents |
first | - | iterator denoting the begin of the range to apply the function to |
n | - | the number of elements to apply the function to |
f | - | the function to apply to the projected range[ first, first+ n) |
proj | - | projection to apply to the elements |
An object{first+ n, std::move(f)}, wherefirst+ n may be evaluated asstd::ranges::next(std::move(first), n) depending on iterator category.
Exactlyn applications off andproj.
struct for_each_n_fn{template<std::input_iterator I,class Proj=std::identity,std::indirectly_unary_invocable<std::projected<I, Proj>> Fun>constexpr for_each_n_result<I, Fun> operator()(I first,std::iter_difference_t<I> n, Fun fun, Proj proj= Proj{})const{for(; n-->0;++first)std::invoke(fun,std::invoke(proj,*first));return{std::move(first), std::move(fun)};}}; inlineconstexpr for_each_n_fn for_each_n{};
#include <algorithm>#include <array>#include <iostream>#include <ranges>#include <string_view> struct P{int first;char second;friendstd::ostream& operator<<(std::ostream& os,const P& p){return os<<'{'<< p.first<<",'"<< p.second<<"'}";}}; auto print=[](std::string_view name,autoconst& v){std::cout<< name<<": ";for(auto n= v.size();constauto& e: v)std::cout<< e<<(--n?", ":"\n");}; int main(){std::array a{1,2,3,4,5}; print("a", a);// Negate first three numbers: std::ranges::for_each_n(a.begin(),3,[](auto& n){ n*=-1;}); print("a", a); std::array s{ P{1,'a'}, P{2,'b'}, P{3,'c'}, P{4,'d'}}; print("s", s);// Negate data members 'P::first' using projection: std::ranges::for_each_n(s.begin(),2,[](auto& x){ x*=-1;},&P::first); print("s", s);// Capitalize data members 'P::second' using projection: std::ranges::for_each_n(s.begin(),3,[](auto& c){ c-='a'-'A';},&P::second); print("s", s);}
Output:
a: 1, 2, 3, 4, 5a: -1, -2, -3, 4, 5s: {1,'a'}, {2,'b'}, {3,'c'}, {4,'d'}s: {-1,'a'}, {-2,'b'}, {3,'c'}, {4,'d'}s: {-1,'A'}, {-2,'B'}, {3,'C'}, {4,'d'}
range-for loop(C++11) | executes loop over range[edit] |
(C++20) | applies a unaryfunction object to elements from arange (algorithm function object)[edit] |
(C++17) | applies a function object to the first N elements of a sequence (function template)[edit] |
applies a unaryfunction object to elements from arange (function template)[edit] |