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,std::weakly_incrementable O> requiresstd::indirectly_copyable<I, O> | (1) | (since C++20) |
template<ranges::input_range R,std::weakly_incrementable O> requiresstd::indirectly_copyable<ranges::iterator_t<R>, O> | (2) | (since C++20) |
template<std::input_iterator I,std::sentinel_for<I> S,std::weakly_incrementable O, class Proj=std::identity, | (3) | (since C++20) |
template<ranges::input_range R,std::weakly_incrementable O, class Proj=std::identity, | (4) | (since C++20) |
Helper types | ||
template<class I,class O> using copy_result=ranges::in_out_result<I, O>; | (5) | (since C++20) |
template<class I,class O> using copy_if_result=ranges::in_out_result<I, O>; | (6) | (since C++20) |
Copies the elements in the range, defined by[
first,
last)
, to another range beginning atresult.
[
first,
last)
starting fromfirst and proceeding tolast-1. The behavior is undefined ifresult is within the range[
first,
last)
. In this case,ranges::copy_backward may be used instead.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 copy |
r | - | the range of elements to copy |
result | - | the beginning of the destination range. |
pred | - | predicate to apply to the projected elements |
proj | - | projection to apply to the elements |
Aranges::in_out_result containing an input iterator equal tolast and an output iterator past the last element copied.
In practice, implementations ofranges::copy
avoid multiple assignments and use bulk copy functions such asstd::memmove if the value type isTriviallyCopyable and the iterator types satisfycontiguous_iterator
.
When copying overlapping ranges,ranges::copy
is appropriate when copying to the left (beginning of the destination range is outside the source range) whileranges::copy_backward
is appropriate when copying to the right (end of the destination range is outside the source range).
copy (1)(2) |
---|
struct copy_fn{template<std::input_iterator I,std::sentinel_for<I> S,std::weakly_incrementable O> requiresstd::indirectly_copyable<I, O>constexpr ranges::copy_result<I, O> operator()(I first, S last, O result)const{for(; first!= last;++first,(void)++result)*result=*first;return{std::move(first), std::move(result)};} template<ranges::input_range R,std::weakly_incrementable O> requiresstd::indirectly_copyable<ranges::iterator_t<R>, O>constexpr ranges::copy_result<ranges::borrowed_iterator_t<R>, O> operator()(R&& r, O result)const{return(*this)(ranges::begin(r),ranges::end(r), std::move(result));}}; inlineconstexpr copy_fn copy; |
copy_if (3)(4) |
struct copy_if_fn{template<std::input_iterator I,std::sentinel_for<I> S,std::weakly_incrementable O,class Proj=std::identity,std::indirect_unary_predicate<std::projected<I, Proj>> Pred> requiresstd::indirectly_copyable<I, O>constexpr ranges::copy_if_result<I, O> operator()(I first, S last, O result, Pred pred, Proj proj={})const{for(; first!= last;++first)if(std::invoke(pred,std::invoke(proj,*first))){*result=*first;++result;}return{std::move(first), std::move(result)};} template<ranges::input_range R,std::weakly_incrementable O,class Proj=std::identity,std::indirect_unary_predicate< std::projected<ranges::iterator_t<R>, Proj>> Pred> requiresstd::indirectly_copyable<ranges::iterator_t<R>, O>constexpr ranges::copy_if_result<ranges::borrowed_iterator_t<R>, O> operator()(R&& r, O result, Pred pred, Proj proj={})const{return(*this)(ranges::begin(r),ranges::end(r), std::move(result),std::ref(pred),std::ref(proj));}}; inlineconstexpr copy_if_fn copy_if; |
The following code usesranges::copy
to both copy the contents of onestd::vector to another and to display the resultingstd::vector
.
#include <algorithm>#include <iostream>#include <iterator>#include <numeric>#include <vector> int main(){std::vector<int> source(10);std::iota(source.begin(), source.end(),0);std::vector<int> destination; std::ranges::copy(source.begin(), source.end(),std::back_inserter(destination)); // or, alternatively,// std::vector<int> destination(source.size());// std::ranges::copy(source.begin(), source.end(), destination.begin());// either way is equivalent to// std::vector<int> destination = source; std::cout<<"Destination contains: "; std::ranges::copy(destination,std::ostream_iterator<int>(std::cout," "));std::cout<<'\n'; std::cout<<"Odd numbers in destination are: "; std::ranges::copy_if(destination,std::ostream_iterator<int>(std::cout," "),[](int x){return(x%2)==1;});std::cout<<'\n';}
Output:
Destination contains: 0 1 2 3 4 5 6 7 8 9Odd numbers in destination are: 1 3 5 7 9
(C++20) | copies a range of elements in backwards order (algorithm function object)[edit] |
(C++20) | creates a copy of a range that is reversed (algorithm function object)[edit] |
(C++20) | copies a number of elements to a new location (algorithm function object)[edit] |
(C++20) | assigns a range of elements a certain value (algorithm function object)[edit] |
(C++20)(C++20) | copies a range of elements omitting those that satisfy specific criteria (algorithm function object)[edit] |
(C++11) | copies a range of elements to a new location (function template)[edit] |