std::ranges::reverse(3)
π std::ranges::reverse π
hey bestie, welcome to the std::ranges::reverse(3) page from the C++ Standard Libary manual β¨ this one is short and iconic: it flips a range around, no cap.
πΈ NAME πΈ
std::ranges::reverse β std::ranges::reverse (yes, the name is literally the name, we love a self-referential moment π)
β¨ Synopsis β¨
Defined in header <algorithm>.
Call signature
template< std::bidirectional_iterator I, std::sentinel_for<I> S >
requires std::permutable<I> (1)
(since C++20)
constexpr I
reverse( I first, S last );
template< ranges::bidirectional_range R >
requires std::permutable<ranges::iterator_t<R>> (2)
(since C++20)
constexpr ranges::borrowed_iterator_t<R>
reverse( R&& r );
1) Reverses the order of the elements in the range [first, last). Behaves as if applying ranges::iter_swap to every pair of iterators first + i, last - i - 1 for each integer i, where 0 β€ i < (last - first) / 2.
2) Same as (1), but uses r as the range, as if using ranges::begin(r) as first and ranges::end(r) as last.
The function-like entities described on this page are niebloids, that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions.
π Parameters π
first,lastβ the range of elements to reverserβ the range of elements to reverse
π¦ Return value π¦
An iterator equal to last.
π Complexity π
Exactly (last - first) / 2 swaps. That's the whole budget, bestie.
π₯ 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. That's the note, periodt.
π§ 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::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 π
ranges::reverse_copyβ creates a copy of a range that is reversed(C++20)(niebloid)ranges::reverse_viewβ a view that iterates over the elements of another bidirectional view in reverse order(C++20)(class template)views::reverseβ reverses the order of elements in a range(C++20)(range adaptor object)reverseβ reverses the order of elements in a range(function template)
mwah π β source/version: http://cppreference.com, date: 2024.06.10, page: std::ranges::reverse(3). Stay reversed, bestie π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms