std::rbegin,std::crbegin(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸŽ€ 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. 1,2) Returns c.begin(), which is typically an iterator to the beginning of the sequence represented by c.
  2. 1) If C is a standard Container, returns a C::reverse_iterator object.
  3. 2) If C is a standard Container, returns a C::const_reverse_iterator object.
  4. 3) Returns a std::reverse_iterator<T*> object to the reverse-beginning of array.
  5. 4) Returns a std::reverse_iterator<const T*> object to the reverse-beginning of il.
  6. 5) Returns std::rbegin(c), with c always treated as const-qualified. If C is a standard Container, returns a C::const_reverse_iterator object.

There’s also a diagram on the page: range-rbegin-rend.svg πŸ–ΌοΈ

🌈 Parameters 🌈

  • c β€” a container or view with a rbegin member function
  • array β€” an array of arbitrary type
  • il β€” an std::initializer_list

πŸŽ€ Return value πŸŽ€

  1. 1,2) c.rbegin()
  2. 3) std::reverse_iterator<T*>(array + N)
  3. 4) std::reverse_iterator<const T*>(il.end())
  4. 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 template
  • end, cend β€” returns an iterator to the end of a container or array; (C++11), (C++14); function template
  • rend, crend β€” returns a reverse end iterator for a container or array; (C++14); function template
  • ranges::rbegin β€” returns a reverse iterator to a range; (C++20); customization point object
  • ranges::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