std::rbegin,std::crbegin(3)
π std::rbegin,std::crbegin(3) β C++ Standard Libary π
okay so bestie, welcome to the std::rbegin / std::crbegin page from the C++ Standard Library. The manual name is C++ Standard Libary (spelling preserved, bestie π), and this whole page is about getting an iterator to the reverse-beginning of a range. Itβs giving backwards-but-make-it-iconic. π
πΈ NAME πΈ
std::rbegin, std::crbegin β thatβs literally the name, bestie. No cap. π
β¨ Synopsis β¨
Defined in header:
<array><deque><flat_map><flat_set><forward_list><iterator><list><map><regex><set><span><string><string_view><unordered_map><unordered_set><vector>
The synopsis is a whole bouquet of overloads, so hereβs every single one with its (since C++14) / (until C++17) / (since C++17) timestamps intact. π«Ά
Overload (1) π
template< class C >
auto rbegin( C& c ) -> decltype(c.rbegin());
(since C++14) (until C++17)
template< class C >
constexpr auto rbegin( C& c ) -> decltype(c.rbegin());
(since C++17)
Overload (2) π¦
template< class C >
auto rbegin( const C& c ) -> decltype(c.rbegin());
(since C++14) (until C++17)
template< class C >
constexpr auto rbegin( const C& c ) -> decltype(c.rbegin());
(since C++17)
Overload (3) πΈ
template< class T, std::size_t N >
std::reverse_iterator<T*> rbegin( T (&array)[N] );
(since C++14) (until C++17)
template< class T, std::size_t N >
constexpr std::reverse_iterator<T*> rbegin( T (&array)[N] );
(since C++17)
Overload (4) π
template< class T >
std::reverse_iterator<const T*> rbegin( std::initializer_list<T> il );
(since C++14) (until C++17)
template< class T >
constexpr std::reverse_iterator<const T*> rbegin( std::initializer_list<T> il );
(since C++17)
Overload (5) π₯
template< class C >
auto crbegin( const C& c ) -> decltype(std::rbegin(c));
(since C++14) (until C++17)
template< class C >
constexpr auto crbegin( const C& c ) -> decltype(std::rbegin(c));
(since C++17)
Returns an iterator to the reverse-beginning of the given range. Periodt. β¨
1,2)Returnsc.begin(), which is typically an iterator to the beginning 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 astd::reverse_iterator<T*>object to the reverse-beginning ofarray.4)Returns astd::reverse_iterator<const T*>object to the reverse-beginning ofil.5)Returnsstd::rbegin(c), withcalways treated as const-qualified. IfCis a standard Container, returns aC::const_reverse_iteratorobject.
Thereβs also a diagram on the page: range-rbegin-rend.svg πΌοΈ
π Parameters π
cβ a container or view with arbeginmember functionarrayβ an array of arbitrary typeilβ anstd::initializer_list
π Return value π
1,2)c.rbegin()3)std::reverse_iterator<T*>(array + N)4)std::reverse_iterator<const T*>(il.end())5)c.rbegin()
π Exceptions π
May throw implementation-defined exceptions. Donβt say I didnβt warn you. π
π¦ Overloads π¦
Custom overloads of rbegin may be provided for classes and enumerations that do not expose a suitable rbegin() member function, yet can be iterated.
Overloads of rbegin found by argument-dependent lookup can be used to customize the behavior of std::ranges::rbegin and (since C++20) std::ranges::crbegin.
π Notes π
The overload for std::initializer_list is necessary because it does not have a member function rbegin. iykyk. π
π§ Example π§
Hereβs the runnable example (// Run this code, as the page says):
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> v = {3, 1, 4};
auto vi = std::rbegin(v); // the type of βviβ is std::vector<int>::reverse_iterator
std::cout << "*vi = " << *vi << '\n';
*std::rbegin(v) = 42; // OK: after assignment v[2] == 42
// *std::crbegin(v) = 13; // error: the location is read-only
int a[] = {-5, 10, 15};
auto ai = std::rbegin(a); // the type of βaiβ is std::reverse_iterator<int*>
std::cout << "*ai = " << *ai << '\n';
auto il = {3, 1, 4};
// the type of βitβ below is std::reverse_iterator<int const*>:
for (auto it = std::rbegin(il); it != std::rend(il); ++it)
std::cout << *it << ' ';
std::cout << '\n';
}
π¨οΈ Output π¨οΈ
*vi = 4
*ai = 15
4 1 3
π See also π
begin,cbeginβ returns an iterator to the beginning of a container or array; (C++11), (C++14); function templateend,cendβ returns an iterator to the end of a container or array; (C++11), (C++14); function templaterend,crendβ returns a reverse end iterator for a container or array; (C++14); function templateranges::rbeginβ returns a reverse iterator to a range; (C++20); customization point objectranges::crbeginβ returns a reverse iterator to a read-only range; (C++20); customization point object
xoxo, your Unix-fluent bestie π β source: http://cppreference.com, version/date: 2024.06.10, page: std::rbegin,std::crbegin(3). Slay responsibly. ππ
π¦ 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