std::basic_...ng::pop_back(3)
std::basic_...ng::pop_back(3) π
okay so this page is from the C++ Standard Libary manual β yes, that's the exact spelling on the tin, bestie, we are not fixing it π . It's giving βtiny member function, big main-character energy.β
πΈ NAME πΈ
std::basic_string::pop_back - std::basic_string::pop_back
Literally the name on the label, no notes. It's the pop_back situation for std::basic_string, and we're obsessed π.
π¦ Synopsis π¦
void pop_back(); (constexpr since C++20)
It removes the last character from the string. It's equivalent to erase(end() - 1).
π¨ Warning, bestie: the behavior is undefined if the string is empty. Not βoops,β not βmaybe fineβ β undefined. Do not test it. Do not poke it. Do not send it a text. π
π Parameters π
(none)
Bestie, there are zero parameters. None. Nada. The function said βI need nothing from youβ and honestly? Iconic. β¨
π Return value π
(none)
It returns nothing. It simply does the thing and leaves. No receipts, no follow-up. π«Ά
π₯ Complexity π₯
Constant.
Constant time, periodt. β
π Exceptions π
Throws nothing.
Good news, bestie: it throws nothing. That's the whole sentence. Slay. π
π§ Notes π§
In libstdc++, pop_back() is not available in C++98 mode.
So likeβ¦ if you're in C++98 mode, this function is not available. That's the note. That's the vibe. π
π Example π
// Run this code
#include <cassert>
#include <iomanip>
#include <iostream>
#include <string>
int main()
{
std::string str("Short string!");
std::cout << "before=" << std::quoted(str) << '\n';
assert(str.size() == 13);
str.pop_back();
std::cout << " after=" << std::quoted(str) << '\n';
assert(str.size() == 12);
str.clear();
// str.pop_back(); // undefined behavior
}
π Output: π
before="Short string!"
after="Short string"
Notice the commented-out str.pop_back(); after str.clear();? That's the undefined behavior warning living right there in the code, bestie. We respect the comment. We do not run the comment. π
π 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_stringdid not have the member functionpop_back()β Correct behavior: added.
So yeah, LWG 534 walked in retroactively and said βactually, let's add that member function.β History was made. π
π¦ See also π¦
push_backβ appends a character to the end (public member function)eraseβ removes characters (public member function)
π«Ά that's the whole page, bestie β sourced from http://cppreference.com, dated 2024.06.10, and filed under std::basic_...ng::pop_back(3). periodt. π
π¦ 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