std::basic_...::push_back(3)
πΈ std::basic_string::push_back β section 3 of the C++ Standard Library man pages π
Hey bestie, ok so listen β this is the man page for std::basic_string::push_back, and honestly? It's giving "tiny function, big main character energy." π
It's the one little member function whose whole entire job is: put a character at the end of your string. That's it. That's the whole vibe. And she does it flawlessly.
β¨ NAME β¨
std::basic_string::push_back β which, yeah, is exactly what it sounds like: std::basic_string::push_back. No plot twist, no lore, just the name matching the name. π«Ά
π Synopsis π
void push_back( CharT ch ); (constexpr since C++20)
What it does: appends the given character ch to the end of the string. That's the whole thing, bestie. One character, added to the end, no cap. β¨
π¦ Parameters π¦
chβ the character you want to append π
π Return value π
(none)
Like, she returns nothing. She's not here for a reward, she's here to do the job. void and unbothered. π
β Complexity β
Amortized constant. So like, usually literally instant, and on average you're so fine. Go off. π₯
π Exceptions π
Ok real talk, but also pay attention here:
- If the operation would make
size() > max_size(), it throwsstd::length_error. That's your hard limit, no cap, can't push past it. π« - If an exception is thrown for any reason, this function has no effect β that's the strong exception safety guarantee. So it's like nothing ever happened. Iconic rollback behaviour, honestly. π
π§ Example π§
// Run this code
#include <iomanip>
#include <iostream>
#include <string>
int main()
{
std::string str{"Short string"};
std::cout << "1) " << std::quoted(str) << ", size: " << str.size() << '\n';
str.push_back('!');
std::cout << "2) " << std::quoted(str) << ", size: " << str.size() << '\n';
}
π Output π
1) "Short string", size: 12
2) "Short string!", size: 13
See how the size went from 12 to 13? That's her. That's the push_back effect. She added exactly one ! and the string ate. ππ
π₯ Defect reports π₯
So the following behavior-changing defect reports were applied retroactively to previously published C++ standards β meaning yes, your old code was living a different timeline. β³
LWG 7β applied to C++98. Behavior as published: (1) the description was missing in the C++ standard, and (2) the parameter type wasconst CharT. Correct behavior: (1) description added, and (2) changed toCharT.LWG 847β applied to C++98. Behavior as published: there was no exception safety guarantee. Correct behavior: added strong exception safety guarantee.
π See also π
pop_backβ removes the last character (public member function). She's the opposite twin, the take it back of the family. π
ngl bestie, the whole lesson is:push_backadds one char to the end, it's amortized constant, it throwsstd::length_errorif you'd blow pastmax_size(), and it's got your back with the strong exception safety guarantee. Go forth and append. π«Άβ¨
π that's the whole page!! π brought to you by http://cppreference.com, dated 2024.06.10 β std::basic_...::push_back(3), logging off, 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