std::basic_..._view::back(3)
β¨ 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 ofstd::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
- 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