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 I,std::sentinel_for<I> S> requiresstd::permutable<I> | (1) | (since C++20) |
template<ranges::bidirectional_range R> requiresstd::permutable<ranges::iterator_t<R>> | (2) | (since C++20) |
[
first,
last)
.i
, where0 ≤ i<(last- first)/2.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 reverse |
r | - | the range of elements to reverse |
An iterator equal tolast.
Exactly(last- first)/2 swaps.
Implementations (e.g.MSVC STL) may enable vectorization when the iterator type modelscontiguous_iterator
and swapping its value type calls neither non-trivial special member function norADL-foundswap
.
See also implementations inlibstdc++ andMSVC STL.
struct reverse_fn{template<std::bidirectional_iterator I,std::sentinel_for<I> S> requiresstd::permutable<I>constexpr I operator()(I first, S last)const{auto last2{ranges::next(first, last)};for(auto tail{last2};!(first== tail or first==--tail);++first)ranges::iter_swap(first, tail);return last2;} template<ranges::bidirectional_range R> requiresstd::permutable<ranges::iterator_t<R>>constexprranges::borrowed_iterator_t<R> operator()(R&& r)const{return(*this)(ranges::begin(r),ranges::end(r));}}; inlineconstexpr reverse_fn reverse{}; |
#include <algorithm>#include <array>#include <iostream>#include <string> int main(){std::string s{"ABCDEF"};std::cout<< s<<" → "; std::ranges::reverse(s.begin(), s.end());std::cout<< s<<" → "; std::ranges::reverse(s);std::cout<< s<<" │ "; std::array a{1,2,3,4,5};for(auto e: a)std::cout<< e<<' ';std::cout<<"→ "; std::ranges::reverse(a);for(auto e: a)std::cout<< e<<' ';std::cout<<'\n';}
Output:
ABCDEF → FEDCBA → ABCDEF │ 1 2 3 4 5 → 5 4 3 2 1
(C++20) | creates a copy of a range that is reversed (algorithm function object)[edit] |
aview that iterates over the elements of another bidirectional view in reverse order(class template)(range adaptor object)[edit] | |
reverses the order of elements in a range (function template)[edit] |