std::deque:...ue::crbegin(3)

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

πŸ’… 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