std::basic_string::rend,(3)
π 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)isnoexceptsince C++11 andconstexprsince C++20(2)isnoexceptsince C++11 andconstexprsince C++20(3)isnoexcept, since C++11, andconstexprsince 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 beginningcrbeginβ (public member function) (C++11)rendβ returns a reverse iterator to the endcrendβ (public member function ofstd::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
- 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