std::basic_...iew::rbegin,(3)

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

πŸ’… NAME

okay so bestie, this is std::basic_string_view::rbegin β€” yes the page title is literally std::basic_...iew::rbegin,(3) because the C++ Standard Library manpage is out here doing the absolute most with abbreviations 😭 β€” and it's about getting a reverse iterator pointed at the first character of the reversed view ✨

(also yes, this lives in the C++ Standard Library, in case the parenthetical vibe check wasn't clear 🫢)


✨ Synopsis

Two overloads, both of them besties since C++17:

constexpr const_reverse_iterator rbegin() const noexcept;   (since C++17)
constexpr const_reverse_iterator crbegin() const noexcept;  (since C++17)

So what are these actually doing? They return a reverse iterator to the first character of the reversed view πŸ”₯ And that corresponds to the last character of the non-reversed view. Not me explaining it in two directions but like β€” that's genuinely what it is πŸ’€

There's also a lil diagram on the page, range-rbegin-rend.svg, if you're a visual learner πŸŽ€


🌸 Parameters

(none)

Literally zero arguments. She takes nothing. Iconic, low-effort, no notes πŸ‘‘


πŸ¦‹ Return value

A const_reverse_iterator to the first character. That's it, that's the whole deal πŸ’–


⭐ Complexity

Constant. Constannnttt. O(1) behavior, main character energy, in and out, no drama 🌈


🌺 Example

Run this code, fr fr:

// Run this code

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string_view>

int main()
{
    std::ostream_iterator<char> out_it(std::cout);
    std::string_view str_view("abcdef");

    std::copy(str_view.rbegin(), std::next(str_view.rbegin(), 3), out_it);
    *out_it = '\n';

    std::copy(str_view.crbegin(), std::next(str_view.crbegin(), 3), out_it);
    *out_it = '\n';
}

πŸ’œ Output

fed
fed

fed twice β€” rbegin() ate and left no crumbs, then crbegin() came through and did the exact same thing πŸ“ (the string is "abcdef", so reversed you're reading it backwards from the f, hence fed β€” iykyk 😌)


🧚 See also

  • rend β€” returns a reverse iterator to the end
  • crend β€” (public member function)
  • rbegin β€” returns a reverse iterator to the beginning
  • crbegin β€” (public member function of std::basic_string<CharT,Traits,Allocator>) (C++11)

like yes, they're the sibling functions, and yes one of them is from std::basic_string not the view, that's just how the page lists it, don't come for me πŸ™ˆ


ngl the docs are honest about nothing here being dangerous β€” no UB, no deprecation, no gotchas, everything's constexpr and noexcept and safe. Slay. πŸ’…

and that's the page!! brought to you by cppreference.com πŸ’– version/date stamp 2024.06.10, and the original footer named it std::basic_...iew::rbegin,(3) β€” so go forth, reverse-iterate responsibly, and remember: last character of the normal view = first character of the reversed view, periodt πŸŽ€βœ¨

πŸ“¦ where this page comes from