std::deque:...ue::crbegin(3)
π std::deque::rbegin, std::deque::crbegin π
okay bestie, welcome to the C++ Standard Library β specifically the std::deque corner of it, where we're looking at std::deque::rbegin and std::deque::crbegin. yes they're basically twins. yes one of them is const. let's get into it β¨
πΈ 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 what do these do? they hand you a reverse iterator pointing at the first element of the reversed deque π¦ β which, plot twist, is the last element of the non-reversed deque. main character energy honestly.
and if the deque is empty, the iterator you get back is equal to rend(). no elements, no drama, it just matches up π
the original docs also have a lil diagram here β range-rbegin-rend.svg β if you wanna see the vibes visually π¨
Parameters
(none) π«Ά
literally nothing. she takes no arguments. low maintenance queen.
Return value
a reverse iterator to the first element. that's it, that's the whole thing π
Complexity
Constant. β‘ O(1). instant. no waiting. we love efficiency in this house.
Notes
okay this part is important so listen up π₯
the underlying iterator of the returned reverse iterator is the end iterator. so if and when the end iterator gets invalidated, your returned iterator gets invalidated too. they're linked, bestie. ride or die. if one goes down, so does the other π
also β libc++ backports crbegin() to C++98 mode. slay of them honestly π
Example
// Run this code
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <deque>
int main()
{
std::deque<int> nums{1, 2, 4, 8, 16};
std::deque<std::string> fruits{"orange", "apple", "raspberry"};
std::deque<char> empty;
// Print deque.
std::for_each(nums.rbegin(), nums.rend(), [](const int n) { std::cout << n << ' '; });
std::cout << '\n';
// Sums all integers in the deque 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 deque fruits, checking if there is any.
if (!fruits.empty())
std::cout << "First fruit: " << *fruits.rbegin() << '\n';
if (empty.rbegin() == empty.rend())
std::cout << "deque 'empty' is indeed empty.\n";
}
β¨ Output
16 8 4 2 1
Sum of nums: 31
First fruit: raspberry
deque 'empty' is indeed empty.
see how it printed 16 8 4 2 1? reversed, because reverse iterators. and *fruits.rbegin() gave us raspberry β the last fruit β because that's how rbegin rolls π adorable.
See also
rendβ returns a reverse iterator to the end (public member function) πcrendβ (public member function) (C++11) πrbeginβ returns a reverse iterator to the beginning of a container or array (function template) βcrbeginβ (C++14) π§
that's the whole page bestie!! no cap, you're now a reverse-iterator girlie π
source: cppreference.com Β· version/date: 2024.06.10 Β· page: std::deque:...ue::crbegin(3) β mwah, go write some const-correct code for me ππ
π¦ 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