std::forwar...::push_front(3)

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

πŸ‘‘ NAME πŸ‘‘

Okay bestie, welcome to std::forward_list::push_front β€” aka std::forward_list::push_front (yes, the name is literally the same thing twice, and we're obsessed). It lives in the C++ Standard Libary, page 3. The header is out here looking like std::forwar...::push_front(3) because truncation said "not me," but the full function name is right above. πŸ’…

πŸ¦‹ Synopsis πŸ¦‹

Two overloads, both since C++11, both ready to prepend. Here's the exact energy:

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

It prepends the given element value to the beginning of the container. That's the whole vibe.

No iterators or references are invalidated. Your existing iterators and refs stay valid, no cap. 🫢

πŸŽ€ Parameters πŸŽ€

  • value β€” the value of the element to prepend. That's it, that's the parameter, and it's serving.

πŸ’– Return value πŸ’–

(none) β€” literally nothing comes back, bestie. It's giving void energy, and we respect it. 😌

⭐ Complexity ⭐

Constant. That's the complexity, periodt. ✨

πŸ”₯ Exceptions πŸ”₯

If an exception is thrown for any reason, these functions have no effect β€” that's the strong exception safety guarantee. So if it throws, it's like it never happened πŸ’…. Dramatic? Maybe. Accurate? Yes.

🌸 Example 🌸

Run this code and watch the forward_list do its little thing:

       // Run this code

        #include <forward_list>
        #include <iomanip>
        #include <iostream>
        #include <string>

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

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

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

🫢 See also 🫢

  • emplace_front β€” constructs an element in-place at the beginning (public member function)
  • pop_front β€” removes the first element (public member function)
  • creates a std::front_insert_iterator of type inferred from the front_inserter argument (function template)

That's the whole page, bestie! Source: http://cppreference.com Β· version/date: 2024.06.10 Β· original footer energy: std::forwar...::push_front(3). Now go prepend something iconic. πŸ’–βœ¨

πŸ“¦ where this page comes from