std::forwar...::push_front(3)
π 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_iteratorof type inferred from thefront_inserterargument (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
- 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