Skip to main content

std::forward_list push_front() method

// Non const version only
void push_front( const T& value );

// Non const version only
void push_front( T&& value );

Prepends the given element value to the beginning of the container.

Parameters

  • value - the value of the element to prepend

Return value

(none)

Complexity

Constant - O(1).

Exceptions

If an exception is thrown, this function has no effect (strong exception guarantee).

Example

Main.cpp
#include <forward_list>
#include <iostream>
#include <iomanip>
#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';
}
Output
std::forward_list `letters` holds: "def" "abc"
Moved-from string `s` holds: ""
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::forward_list push_front() method

// Non const version only
void push_front( const T& value );

// Non const version only
void push_front( T&& value );

Prepends the given element value to the beginning of the container.

Parameters

  • value - the value of the element to prepend

Return value

(none)

Complexity

Constant - O(1).

Exceptions

If an exception is thrown, this function has no effect (strong exception guarantee).

Example

Main.cpp
#include <forward_list>
#include <iostream>
#include <iomanip>
#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';
}
Output
std::forward_list `letters` holds: "def" "abc"
Moved-from string `s` holds: ""
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.