std::basic_string::back(3)

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

🌸 NAME 🌸

Hiii bestie πŸ’– welcome to std::basic_string::back(3), straight outta the C++ Standard Libary. The name is literally std::basic_string::back β€” and yeah, it’s also std::basic_string::back, iconic behavior.

✨ Synopsis ✨

CharT& back();             (1) (constexpr since C++20)
const CharT& back() const; (2) (constexpr since C++20)

Returns a reference to the last character in the string. The behavior is undefined if empty() is true β€” like, do not do that, bestie πŸ˜­πŸ’€.

πŸŽ€ Parameters πŸŽ€

  • (none) β€” literally no parameters, she takes nothing.

πŸ’… Return value πŸ’…

Reference to the last character, equivalent to operator[](size() - 1). Slay.

πŸ¦‹ Complexity πŸ¦‹

Constant. No cap.

🌸 Notes 🌸

In libstdc++, back() is not available in C++98 mode. Periodt.

πŸ”₯ Example πŸ”₯

// Run this code

#include <iostream>
#include <string>

int main()
{
    std::string s("Exemplary");
    char& back1 = s.back();
    back1 = 's';
    std::cout << s << '\n'; // "Exemplars"

    std::string const c("Exemplary");
    char const& back2 = c.back();
    std::cout << back2 << '\n'; // 'y'
}

🌈 Output 🌈

Exemplars
y

πŸ‘‘ Defect reports πŸ‘‘

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR: LWG 534 Β· Applied to: C++98 Β· Behavior as published: std::basic_string did not have the member function back() Β· Correct behavior: added

πŸ’œ See also πŸ’œ

  • front β€” accesses the first character (DR*) (public member function)
  • back β€” accesses the last character (public member function of std::basic_string_view<CharT,Traits>)

http://cppreference.com Β· 2024.06.10 Β· std::basic_string::back(3) β€” bye bestie, go slay your strings πŸ’…βœ¨πŸ«Ά

πŸ“¦ where this page comes from