std::rend,std::crend(3)
π NAME π
std::rend, std::crend β the reverse-end iterator duo, aka the "bestie, go backwards" squad π
(Yeah the original page literally just says std::rend,std::crend in the NAME section and honestly? Iconic minimalism. But you're getting the full story below.)
β¨ SYNOPSIS β¨
So first things first, these functions live in a lot of headers. Like, a concerning amount. It's giving "shows up everywhere" π
Defined in header <array>
Defined in header <deque>
Defined in header <flat_map>
Defined in header <flat_set>
Defined in header <forward_list>
Defined in header <iterator>
Defined in header <list>
Defined in header <map>
Defined in header <regex>
Defined in header <set>
Defined in header <span>
Defined in header <string>
Defined in header <string_view>
Defined in header <unordered_map>
Defined in header <unordered_set>
Defined in header <vector>
Okay now the actual signatures. Buckle up, there's five of them, numbered, and the version tags are doing the most π«
template< class C >
auto rend( C& c ) ->
decltype(c.rend());
(since C++14), (until C++17)
template< class C >
constexpr auto rend( C& c ) ->
decltype(c.rend());
(since C++17)
template< class C >
auto rend( const C& c ) ->
decltype(c.rend());
(since C++14), (until C++17)
template< class C >
constexpr auto rend( const C& c ) ->
decltype(c.rend());
(since C++17)
Overload (1):
template< class T, std::size_t N >
std::reverse_iterator<T*> rend( T (&array)[N] );
(since C++14), (until C++17)
template< class T, std::size_t N >
constexpr std::reverse_iterator<T*> rend( T (&array)[N] );
(since C++17)
Overload (2):
template< class T >
std::reverse_iterator<const T*> rend( std::initializer_list<T> il );
(since C++14), (until C++17)
Overload (3):
template< class T >
constexpr std::reverse_iterator<const T*> rend( std::initializer_list<T> il );
(since C++17)
Overload (4):
template< class C >
auto crend( const C& c ) ->
decltype(std::rend(c));
(since C++14), (until C++17)
Overload (5):
template< class C >
constexpr auto crend( const C& c ) -> decltype(std::rend(c));
(since C++17)
π¦ What it actually does π¦
Returns an iterator to the reverse-end of the given range. No cap, that's the whole vibe. It's the "you've reached the end of going backwards" marker π₯
1,2)Returnsc.rend(), which is typically an iterator one past the reverse-end of the sequence represented byc.1)IfCis a standard Container, returns aC::reverse_iteratorobject.2)IfCis a standard Container, returns aC::const_reverse_iteratorobject.
3)Returns anstd::reverse_iterator<T*>object to the reverse-end ofarray.4)Returns anstd::reverse_iterator<const T*>object to the reverse-end ofil.5)Returnsstd::end(c), withcalways treated as const-qualified. IfCis a standard Container, returns aC::const_reverse_iteratorobject.
Also, the original page has a whole diagram in here called range-rbegin-rend.svg β if you're reading this on cppreference dot com, that's the little picture showing you which way round everything goes π
π PARAMETERS π
cβ a container or view with arendmember functionarrayβ an array of arbitrary typeilβ anstd::initializer_list
π RETURN VALUE π
1,2)c.rend()3)std::reverse_iterator<T*>(array)4)std::reverse_iterator<const T*>(il.begin())5)c.rend()
π EXCEPTIONS π
May throw implementation-defined exceptions. So like... ask your implementation. It knows. We don't π
π Overloads π
Custom overloads of rend may be provided for classes and enumerations that do not expose a suitable rend() member function, yet can be iterated. Slay, you do you π
(since C++20) β Overloads of rend found by argument-dependent lookup can be used to customize the behavior of std::ranges::rend and std::ranges::crend. ADL customization, we love to see it β¨
πΈ NOTES πΈ
The overload for std::initializer_list is necessary because it does not have a member function rend. That's literally the entire reason it exists β it's a workaround bestie and it's thriving π§
π EXAMPLE π
// Run this code
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
int a[]{4, 6, -3, 9, 10};
std::cout << "C-style array `a` backwards: ";
std::copy(std::rbegin(a), std::rend(a),
std::ostream_iterator<int>(std::cout, " "));
auto il = {3, 1, 4};
std::cout << "\nstd::initializer_list `il` backwards: ";
std::copy(std::rbegin(il), std::rend(il),
std::ostream_iterator<int>(std::cout, " "));
std::vector<int> v{4, 6, -3, 9, 10};
std::cout << "\nstd::vector `v` backwards: ";
std::copy(std::rbegin(v), std::rend(v),
std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
β Output β
C-style array `a` backwards: 10 9 -3 6 4
std::initializer_list `il` backwards: 4 1 3
std::vector `v` backwards: 10 9 -3 6 4
π«Ά SEE ALSO π«Ά
end,cend(C++11),(C++14)β returns an iterator to the end of a container or array (function template)rbegin,crbegin(C++14)β returns a reverse iterator to the beginning of a container or array (function template)begin,cbegin(C++11),(C++14)β returns an iterator to the beginning of a container or array (function template)ranges::rend(C++20)β returns a reverse end iterator to a range (customization point object)ranges::crend(C++20)β returns a reverse end iterator to a read-only range (customization point object)
π Okay that's the whole page, bestie. You ate that. Go forth and iterate backwards with confidence.
Fr fr, this one's from http://cppreference.com, page std::rend,std::crend(3), dated 2024.06.10. Mwah π
π¦ 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