std::basic_...::push_back(3)

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

🌸 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 throws std::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 was const CharT. Correct behavior: (1) description added, and (2) changed to CharT.
  • 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_back adds one char to the end, it's amortized constant, it throws std::length_error if you'd blow past max_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