std::vector::push_back(3)

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

πŸ’– std::vector::push_back β€” the glow-up edit ✨

okay bestie, so this is the <vector> glow-up manual okay, from the C++ Standard Library, and it's literally about adding stuff to the end of your vector. it's giving "main character energy" for your containers πŸ’…


✨ NAME ✨

std::vector::push_back β€” because sometimes you just gotta append, no cap πŸŽ€


🌸 Synopsis 🌸

void push_back( const T& value ); (1) (constexpr since C++20)
void push_back( T&& value );      (2) (since C++11)
                                      (constexpr since C++20)

okay so what this does is append the element you pass as value to the end of the container. periodt.

  1. the new element gets initialized as a copy of value
  2. value gets moved into the new element (this one's the C++11 glow-up πŸ’–)

now here's the tea 🍡 β€” if after the operation the new size() is greater than the old capacity(), a reallocation happens, which means all iterators (including the end() iterator) and all references to the elements get invalidated. otherwise, only the end() iterator is invalidated. so watch your iterators, bestie, they can literally get left on read πŸ“΅


🌈 Parameters 🌈

  • value β€” the value of the element to append, aka the thing you're yeeting onto the end πŸ¦‹

🧚 Type requirements 🧚

  • T must meet the requirements of CopyInsertable in order to use overload (1)
  • T must meet the requirements of MoveInsertable in order to use overload (2)

vibe check on your type, bestie, make sure it slays those requirements πŸ‘‘


πŸ’œ Return value πŸ’œ

(none) β€” it does the thing and gives you nothing back. silent. mysterious. iconic. πŸ’€


⭐ Complexity ⭐

Amortized constant. so like, quick on average, no stressful math required 🫢


πŸ”₯ Exceptions πŸ”₯

if an exception is thrown (which can be due to Allocator::allocate() or element copy/move constructor/assignment), this function has no effect β€” that's the strong exception guarantee, and she ate with that one πŸ’…

since C++11 note: if T's move constructor is not noexcept and T is not CopyInsertable into *this, vector will use the throwing move constructor. if it throws, the guarantee is waived and the effects are unspecified. 😭 this is the dangerous part, read it twice, iykyk.

🌸 Notes 🌸

some implementations throw std::length_error when push_back causes a reallocation that exceeds max_size (due to an implicit call to an equivalent of reserve(size() + 1)). ngl, good to know πŸ™ˆ


πŸŽ€ Example πŸŽ€

// Run this code

#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> letters;

    letters.push_back("abc");
    std::string s{"def"};
    letters.push_back(std::move(s));

    std::cout << "std::vector letters holds: ";
    for (auto&& e : letters)
        std::cout << std::quoted(e) << ' ';

    std::cout << "\nMoved-from string s holds: " << std::quoted(s) << '\n';
}

⭐ Possible output ⭐

std::vector letters holds: "abc" "def"
Moved-from string s holds: ""

see how s got moved-from and turned into an empty string? that's the move in action, bestie, it ate and left no crumbs πŸ”₯


🌈 See also 🌈

  • emplace_back β€” constructs an element in-place at the end (C++11) (public member function) πŸŽ€
  • pop_back β€” removes the last element (public member function) πŸ’…
  • back_inserter β€” creates a std::back_insert_iterator of type inferred from the argument (function template) πŸ¦‹

so yeah, that's the whole page, fr fr. the source is http://cppreference.com, version 2024.06.10, and this was std::vector::push_back(3) β€” bestie, you're now fully immaculate with push_back. slay. πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά

πŸ“¦ where this page comes from