std::vector::push_back(3)
π 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.
- the new element gets initialized as a copy of
value valuegets 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 π§
Tmust meet the requirements ofCopyInsertablein order to use overload (1)Tmust meet the requirements ofMoveInsertablein 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: ifT's move constructor is notnoexceptandTis notCopyInsertableinto*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 astd::back_insert_iteratorof 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
- 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