std::ranges::reverse
Min standard notice:
Header: <algorithm>
- Reverses the order of the elements in the range [first,last).
# Declarations
Call signature
template< std::bidirectional_iterator I, std::sentinel_for<I> S >
requires std::permutable<I>
constexpr I
reverse( I first, S last );
(since C++20)
template< ranges::bidirectional_range R >
requires std::permutable<ranges::iterator_t<R>>
constexpr ranges::borrowed_iterator_t<R>
reverse( R&& r );
(since C++20)
# Parameters
first, last: the range of elements to reverser: the range of elements to reverse
# Return value
An iterator equal to last.
# 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.
# 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';
}