std::list::...st::crbegin(3)
π 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
- 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