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::bidirectional_iterator I1,std::sentinel_for<I1> S1, std::bidirectional_iterator I2> | (1) | (since C++20) |
template<ranges::bidirectional_range R,std::bidirectional_iterator I> requiresstd::indirectly_copyable<ranges::iterator_t<R>, I> | (2) | (since C++20) |
Helper types | ||
template<class I1,class I2> using copy_backward_result=ranges::in_out_result<I1, I2>; | (3) | (since C++20) |
[
first,
last)
, to another range[
d_last- N,
d_last)
, whereN=ranges::distance(first, last). The elements are copied in reverse order (the last element is copied first), but their relative order is preserved. The behavior is undefined ifd_last is within(first,last]
. In such a casestd::ranges::copy can 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 from |
r | - | the range of the elements to copy from |
d_last | - | the end of the destination range |
{last, d_last- N}
ExactlyN assignments.
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).
struct copy_backward_fn{template<std::bidirectional_iterator I1,std::sentinel_for<I1> S1,std::bidirectional_iterator I2> requiresstd::indirectly_copyable<I1, I2>constexpr ranges::copy_backward_result<I1, I2> operator()(I1 first, S1 last, I2 d_last)const{ I1 last1{ranges::next(first, std::move(last))};for(I1 i{last1}; i!= first;)*--d_last=*--i;return{std::move(last1), std::move(d_last)};} template<ranges::bidirectional_range R,std::bidirectional_iterator I> requiresstd::indirectly_copyable<ranges::iterator_t<R>, I>constexpr ranges::copy_backward_result<ranges::borrowed_iterator_t<R>, I> operator()(R&& r, I d_last)const{return(*this)(ranges::begin(r),ranges::end(r), std::move(d_last));}}; inlineconstexpr copy_backward_fn copy_backward{}; |
#include <algorithm>#include <iostream>#include <ranges>#include <string_view>#include <vector> void print(std::string_view rem, std::ranges::forward_rangeautoconst& r){for(std::cout<< rem<<": ";autoconst& elem: r)std::cout<< elem<<' ';std::cout<<'\n';} int main(){constauto src={1,2,3,4}; print("src", src); std::vector<int> dst(src.size()+2); std::ranges::copy_backward(src, dst.end()); print("dst", dst); std::ranges::fill(dst,0);constauto[in, out]= std::ranges::copy_backward(src.begin(), src.end()-2, dst.end()); print("dst", dst); std::cout<<"(in - src.begin) == "<<std::distance(src.begin(), in)<<'\n'<<"(out - dst.begin) == "<<std::distance(dst.begin(), out)<<'\n';}
Output:
src: 1 2 3 4dst: 0 0 1 2 3 4dst: 0 0 0 0 1 2(in - src.begin) == 2(out - dst.begin) == 4
(C++20)(C++20) | copies a range of elements to a new location (algorithm function object)[edit] |
(C++20) | copies a number of elements to a new location (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] |
copies a range of elements in backwards order (function template)[edit] |