std::list::push_back(3)

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

std::list::push_back(3) πŸ’–

okay bestie, welcome to the C++ Standard Libary page for std::list::push_back ✨ this function is lowkey iconic: it just yeets a value onto the end of a list, and we're obsessed. no cap, it's giving tiny-but-mighty main character energy 🫢


πŸ’… NAME πŸ’…

std::list::push_back β€” std::list::push_back. yes bestie, the name really is the name πŸ’€

✨ Synopsis ✨

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

okay so here's the whole vibe: it appends the given element value to the end of the container.

  1. the new element is initialized as a copy of value.
  2. value is moved into the new element.

and the part that matters: no iterators or references are invalidated. periodt. πŸ’…

🌸 Parameters 🌸

  • value β€” the value of the element to append πŸŽ€

πŸ¦‹ 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).

πŸŽ€ Return value πŸŽ€

(none) β€” literally nothing, bestie, it's voiding in the cutest way 🫢

πŸ”₯ Complexity πŸ”₯

Constant. that's it. no extra drama.

πŸ’€ 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, so you can breathe, bestie πŸ˜­πŸ’–

🌈 Example 🌈

okay so here's the code, bestie β€” run it, it's cute:

       // Run this code

        #include <iomanip>
        #include <iostream>
        #include <string>
        #include <list>

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

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

            std::cout << "std::list 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::list letters holds: "abc" "def"
        Moved-from string s holds: ""

πŸ‘‘ See also πŸ‘‘

  • emplace_back β€” constructs an element in-place at the end (C++11) (public member function) ✨
  • push_front β€” inserts an element to the beginning (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) πŸ¦‹

xoxo, your Unix bestie πŸ’– β€” source: http://cppreference.com, version/date: 2024.06.10, page: std::list::push_back(3) πŸŽ€βœ¨

πŸ“¦ where this page comes from