std::basic_..._view::back(3)

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

✨ std::basic_..._view::back(3) ✨

okay so welcome to the std::basic_..._view::back(3) page from the C++ Standard Libary manual β€” yes, that's the exact manual name, typo and all β€” and this is your bestie's guide to std::basic_string_view::back. πŸ’…


πŸŽ€ NAME πŸŽ€

std::basic_string_view::back - std::basic_string_view::back

like, the NAME line is literally that twice, and honestly? duplicate main character energy. periodt. 🫢


🌸 Synopsis 🌸

constexpr const_reference back() const;  (since C++17)

so this is the signature, bestie: it's constexpr, it returns const_reference, it's const, and it's been here since C++17. ✨

Returns reference to the last character in the view. The behavior is undefined if empty() == true.

🚨 real talk: undefined behavior if empty() == true is not a cute look. That's the warning, fr fr. Do not pretend it's fine. 🚨

🫢 Parameters 🫢

(none) β€” literally no parameters. zero. nada. the function said "I need nothing from you," and honestly? respect. πŸ’–


πŸ’– Return value πŸ’–

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

so yeah, it's giving "last character access" via operator[](size() - 1). no cap. πŸ¦‹


⚑ Complexity ⚑

Constant.

that's the whole section. Constant. iconic. no notes. πŸ‘‘


πŸ¦‹ Example πŸ¦‹

// Run this code

        #include <iostream>
        #include <string_view>

        int main()
        {
            for (std::string_view str{"ABCDEF"}; !str.empty();
       str.remove_suffix(1))
                std::cout << str.back() << ' ' << str << '\n';
        }

this example is lowkey a little loop flex: it starts with "ABCDEF", prints str.back() and the current str, then removes one suffix char each time until the view is empty. the way it shrinks is honestly so satisfying. πŸ“

πŸ“ Output πŸ“

F ABCDEF
E ABCDE
D ABCD
C ABC
B AB
A A

look at that perfect little staircase. math ate. πŸ’…


πŸ‘‘ See also πŸ‘‘

  • front β€” accesses the first character (public member function)
  • empty β€” checks whether the view is empty (public member function)
  • back β€” accesses the last character (DR*) (public member function of std::basic_string<CharT,Traits,Allocator>)

iykyk, these are the related member functions, and yes the last one has that (DR*) tag and the full std::basic_string<CharT,Traits,Allocator> type. 🌈


πŸ’Œ and that's the cute little footer flex, bestie: http://cppreference.com Β· 2024.06.10 Β· std::basic_..._view::back(3). source/version/date, all present and accounted for. now go access that last character responsibly. πŸŽ€βœ¨

πŸ“¦ where this page comes from