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::weakly_incrementable O> requiresstd::indirectly_copyable<I, O> | (1) | (since C++20) |
Helper type | ||
template<class I,class O> using copy_n_result=ranges::in_out_result<I, O>; | (2) | (since C++20) |
[
0,
n)
. The behavior is undefined ifresult is within the range[
first,
first+ n)
(ranges::copy_backward might be used instead in this case).The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:
Contents |
first | - | the beginning of the range of elements to copy from |
n | - | number of the elements to copy |
result | - | the beginning of the destination range |
ranges::copy_n_result{first+ n, result+ n} or more formally, a value of typeranges::in_out_result that contains aninput_iterator
iterator equals toranges::next(first, n) and aweakly_incrementable
iterator equals toranges::next(result, n).
Exactlyn assignments.
In practice, implementations ofstd::ranges::copy_n
may avoid multiple assignments and use bulk copy functions such asstd::memmove if the value type isTriviallyCopyable and the iterator types satisfycontiguous_iterator
. Alternatively, such copy acceleration can be injected during an optimization phase of a compiler.
When copying overlapping ranges,std::ranges::copy_n
is appropriate when copying to the left (beginning of the destination range is outside the source range) whilestd::ranges::copy_backward is appropriate when copying to the right (end of the destination range is outside the source range).
struct copy_n_fn{template<std::input_iterator I,std::weakly_incrementable O> requiresstd::indirectly_copyable<I, O>constexpr ranges::copy_n_result<I, O> operator()(I first,std::iter_difference_t<I> n, O result)const{for(; n-->0;(void)++first,(void)++result)*result=*first; return{std::move(first), std::move(result)};}}; inlineconstexpr copy_n_fn copy_n{}; |
#include <algorithm>#include <iomanip>#include <iostream>#include <iterator>#include <string>#include <string_view> int main(){conststd::string_view in{"ABCDEFGH"};std::string out; std::ranges::copy_n(in.begin(),4,std::back_inserter(out));std::cout<<std::quoted(out)<<'\n'; out="abcdefgh";constauto res{std::ranges::copy_n(in.begin(),5, out.begin())};constauto i{std::distance(std::begin(in), res.in)};constauto j{std::distance(std::begin(out), res.out)};std::cout<<"in["<< i<<"] = '"<< in[i]<<"'\n"<<"out["<< j<<"] = '"<< out[j]<<"'\n";}
Output:
"ABCD"in[5] = 'F'out[5] = 'f'
(C++20)(C++20) | copies a range of elements to a new location (algorithm function object)[edit] |
(C++20) | copies a range of elements in backwards order (algorithm function object)[edit] |
(C++20)(C++20) | copies a range of elements omitting those that satisfy specific criteria (algorithm function object)[edit] |
(C++20)(C++20) | copies a range, replacing elements satisfying specific criteria with another value (algorithm function object)[edit] |
(C++20) | creates a copy of a range that is reversed (algorithm function object)[edit] |
(C++20) | copies and rotate a range of elements (algorithm function object)[edit] |
(C++20) | creates a copy of some range of elements that contains no consecutive duplicates (algorithm function object)[edit] |
(C++20) | moves a range of elements to a new location (algorithm function object)[edit] |
(C++20) | moves a range of elements to a new location in backwards order (algorithm function object)[edit] |
(C++11) | copies a number of elements to a new location (function template)[edit] |