[ Web Proxy ]
URL:
Viewing: https://cppreference.com/cpp/algorithm/ranges/reverse [Back]  [Original]

std::ranges::reverse - cppreference.com
cppreference.com
Namespaces
Variants

std::ranges::reverse

From cppreference.com
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Non-modifying sequence operations    
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
(C++11)    

Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations


 
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Fold operations (Helper templates)
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
Specialized <memory> algorithms
Return types
 
Defined in header <algorithm>
Call signature
template< std::bidirectional_iterator I, std::sentinel_for<I> S >
    requires std::permutable<I>
constexpr I reverse( I first, S last );
(1) (since C++20)
template< ranges::bidirectional_range R >
    requires std::permutable<ranges::iterator_t<R>>
constexpr ranges::borrowed_iterator_t<R> reverse( R&& r );
(2) (since C++20)
template< /*execution-policy*/ Ep,
          std::random_access_iterator I, std::sized_sentinel_for<I> S >
    requires std::permutable<I>
I reverse( Ep&& policy, I first, S last );
(3) (since C++26)
template< /*execution-policy*/ Ep, /*sized-random-access-range*/ R >
    requires std::permutable<ranges::iterator_t<R>>
ranges::borrowed_iterator_t<R> reverse( Ep&& policy, R&& r );
(4) (since C++26)

For the definition of /*execution-policy*/, see this page; for the definition of /*sized-random-access-range*/, see this page.

1,2) Reverses the order of the elements in the target range [firstlast) or r by swapping each pair of corresponding elements using std::iter_swap.
3,4) Same as (1,2), but executed according to policy.

The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:

Parameters

first, last - the iterator-sentinel pair defining the target range
r - the target range
policy - the execution policy to use

Return value

The past-the-end iterator of the target range.

Complexity

Given \(\scriptsize N\)N as ranges::distance(first, last) or ranges::distance(r):

1-4) Exactly \(\scriptsize \left\lfloor\frac N2 \right\rfloor\)Template:mrfac swaps.

Exceptions

3,4) During the execution process:
  • If the temporary memory resources required for parallelization are not available, std::bad_alloc is thrown.
  • If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for standard policies, std::terminate is invoked).

Notes

Implementations (e.g. MSVC STL) may enable vectorization when the iterator type models contiguous_iterator and swapping its value type calls neither non-trivial special member function nor ADL-found swap.

Possible implementation

See also implementations in libstdc++ and MSVC STL.

struct reverse_fn
{
    template<std::bidirectional_iterator I, std::sentinel_for<I> S>
        requires std::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>
        requires std::permutable<ranges::iterator_t<R>>
    constexpr ranges::borrowed_iterator_t<R>
        operator()(R&& r) const
    {
        return (*this)(ranges::begin(r),
                       ranges::next(ranges::begin(r), ranges::end(r)));
    }
};

inline constexpr reverse_fn reverse{};

Example

#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

See also

reverses the order of elements in a range
(function template) [edit]
creates a copy of a range that is reversed
(algorithm function object)[edit]
a view that iterates over the elements of another bidirectional view in reverse order
(class template) (range adaptor object)[edit]

Web Proxy Viewer  |  New URL  |  Original Page