std::basic_string::rend,(3)

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

πŸŽ€ std::basic_string::rend,(3) β€” C++ Standard Library vibes πŸŽ€

okay bestie, welcome to the C++ Standard Library corner of the group chat πŸ’… this page is all about std::basic_string::rend, β€” yes the title literally has a comma in it, we don't make the rules, we just slay ✨


✨ NAME ✨

std::basic_string::rend, β€” literally std::basic_string::rend, 🫢 that's the name, comma and all, no cap.


πŸ¦‹ Synopsis πŸ¦‹

reverse_iterator rend();                       (1) (noexcept since C++11)
                                                    (constexpr since C++20)
const_reverse_iterator rend() const;           (2) (noexcept since C++11)
                                                    (constexpr since C++20)
const_reverse_iterator crend() const noexcept; (3) (since C++11)
                                                    (constexpr since C++20)

so we've got three overloads doing their lil reverse-iterator thing πŸ’– overload (1) is the non-const rend(), overload (2) is the const rend(), and overload (3) is crend(), which is noexcept and showed up in C++11 πŸ‘‘

quick vibe check on the annotations, because they matter:

  • (1) is noexcept since C++11 and constexpr since C++20
  • (2) is noexcept since C++11 and constexpr since C++20
  • (3) is noexcept, since C++11, and constexpr since C++20

🌸 Description 🌸

Returns a reverse iterator to the character following the last character of the reversed string πŸ” It corresponds to the character preceding the first character of the non-reversed string.

that character is literally just a placeholder, bestie β€” attempting to access it results in undefined behavior 😭 so don't do it, fr fr.

there's also a diagram referenced in the original as range-rbegin-rend.svg πŸ¦‹ imagine a cute lil range picture right here.


🌺 Parameters 🌺

none 😌 zero. nada. no arguments. it's giving low-maintenance icon.


πŸ’– Return value πŸ’–

Reverse iterator to the character following the last character ✨ that's it, that's the whole return value.


⭐ Complexity ⭐

Constant πŸ”₯ O(1) energy, no cap.


🧚 Notes 🧚

libc++ backports crend() to C++98 mode πŸ‘€ that's the note. that's the whole note. iconic behavior honestly.


πŸ”₯ Example πŸ”₯

// Run this code

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>

int main()
{
    std::string p("[A man, a plan, a canal: Panama]");
    std::string q;

    std::copy(p.crbegin(), p.crend(), std::back_inserter(q));
    std::cout << "q = " << q << '\n';

    std::copy(q.crbegin(), q.crend(), p.rbegin());
    std::cout << "p = " << p << '\n';
}

🌈 Output 🌈

q = ]amanaP :lanac a ,nalp a ,nam A[
p = ]amanaP :lanac a ,nalp a ,nam A[

look at that palindrome getting reversed twice and ending up exactly where it started 😌 ate and left no crumbs πŸ’…


πŸ‘‘ See also πŸ‘‘

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

shoutout to http://cppreference.com for the source, and the 2024.06.10 date on the original page β€” this was std::basic_string::rend,(3), mwah πŸ«ΆπŸ’œ

πŸ“¦ where this page comes from