std::list::...st::crbegin(3)

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

πŸ’… NAME πŸ’…

std::list::rbegin, std::list::crbegin β€” that’s the name, bestie, and this is the C++ Standard Library manual page for it. The header is out here spelling the manual name as C++ Standard Libary and repeating the page title like std::list::rbegin,std::list::crbegin - std::list::rbegin,std::list::crbegin, and honestly? Commitment. πŸ’…


πŸŽ€ Synopsis πŸŽ€

reverse_iterator rbegin();                       (1) (noexcept since C++11)
const_reverse_iterator rbegin() const;           (2) (noexcept since C++11)
const_reverse_iterator crbegin() const noexcept; (3) (since C++11)

So rbegin() gives you a reverse iterator to the first element of the reversed list. That same iterator corresponds to the last element of the non-reversed list. And if the list is empty? The returned iterator is equal to rend(). Cute, consistent, no surprises. πŸ’–

There’s also a diagram literally named range-rbegin-rend.svg in the original, doing the visual explanation thing.


🌸 Parameters 🌸

(none) β€” literally no parameters. Not a single one. πŸ’€


✨ Return value ✨

Reverse iterator to the first element. That’s the return value, periodt.


πŸ¦‹ Complexity πŸ¦‹

Constant. That’s the whole vibe.


πŸ’œ Notes πŸ’œ

The underlying iterator of the returned reverse iterator is the end iterator. So the returned iterator is invalidated if and when the end iterator is invalidated β€” that’s the caveat, bestie, keep it in mind. ⚠️

libc++ backports crbegin() to C++98 mode. Slay for backward compatibility. πŸ’œ


πŸ“ Example πŸ“

Here’s the example from the page. It starts with the literal comment // Run this code, so we’re keeping that exactly:

// Run this code

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <list>

int main()
{
    std::list<int> nums{1, 2, 4, 8, 16};
    std::list<std::string> fruits{"orange", "apple", "raspberry"};
    std::list<char> empty;

    // Print list.
    std::for_each(nums.rbegin(), nums.rend(), [](const int n) {
       std::cout << n << ' '; });
    std::cout << '\n';

    // Sums all integers in the list nums (if any), printing only the
       result.
    std::cout << "Sum of nums: "
                      << std::accumulate(nums.rbegin(), nums.rend(), 0) <<
       '\n';

    // Prints the first fruit in the list fruits, checking if there is
       any.
    if (!fruits.empty())
        std::cout << "First fruit: " << *fruits.rbegin() << '\n';

    if (empty.rbegin() == empty.rend())
        std::cout << "list 'empty' is indeed empty.\n";
}

⭐ Output ⭐

16 8 4 2 1
Sum of nums: 31
First fruit: raspberry
list 'empty' is indeed empty.

🌈 See also 🌈

  • rend β€” returns a reverse iterator to the end πŸ’œ
  • crend β€” public member function, (C++11) ✨
  • rbegin β€” returns a reverse iterator to the beginning of a container or array 🌸
  • crbegin β€” function template, (C++14) πŸ¦‹

source/version: http://cppreference.com β€’ date: 2024.06.10 β€’ page: std::list::...st::crbegin(3) β€” byeee, go reverse-iterate something cute. 🫢✨

πŸ“¦ where this page comes from