C++ std::list::push_front
The std::list::push_front
function adds a new element to the beginning of a std::list
. The new element is copied or moved into the list. This operation is efficient because it does not require reallocation of elements like in some other containers (e.g., std::vector
).
Syntax of std::list::push_front
void push_front(const value_type& val);
void push_front(value_type&& val);
Parameters
Parameter | Description |
---|---|
val | The value of the element to be added to the front of the list. It can be passed as a constant reference or an rvalue. |
Return Value
This function does not return a value. It modifies the list by adding a new element at the beginning.
Exceptions
The std::list::push_front
function may throw exceptions if memory allocation fails or if the copy/move constructor of the element being added throws an exception.
The std::list::push_front
function ensures strong exception safety. If an exception is thrown (e.g., due to memory allocation failure or the copy/move constructor of the element throwing an exception), the state of the list remains unchanged.
Examples for std::list::push_front
Example 1: Adding a Single Element to the Front
This example demonstrates how to use push_front
to add an element to the front of a list:
#include <iostream>
#include <list>
int main() {
std::list<int> myList = {20, 30, 40};
myList.push_front(10); // Add 10 to the front of the list
std::cout << "List contents: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- A
std::list
namedmyList
is initialized with the elements{20, 30, 40}
. - The function
myList.emplace_front(10)
is used to add the element10
to the front of the list. This modifies the list by inserting10
as the first element. - The program uses a range-based
for
loop to iterate over all elements of the list, and print them to output.
Output:
List contents: 10 20 30 40
Example 2: Adding Complex Objects
This example demonstrates how to use push_front
to add a complex object to the front of a list:
#include <iostream>
#include <list>
#include <string>
struct Person {
std::string name;
int age;
Person(const std::string& name, int age) : name(name), age(age) {}
};
int main() {
std::list<Person> people;
people.push_front(Person("Arjun", 25)); // Add a Person object to the front
for (const auto& person : people) {
std::cout << person.name << " (" << person.age << " years old)" << std::endl;
}
return 0;
}
Explanation:
- A
Person
struct is defined with two members:name
: A string representing the person’s name.age
: An integer representing the person’s age.- constructor that initializes its members using parameters:
- A
std::list
ofPerson
objects namedpeople
is created and initially empty. - The function
push_front
is used to construct and add aPerson
object to the front of the list:people.push_front("Arjun", 25)
: Constructs aPerson
object with the name"Arjun"
and age25
directly in the list without creating a temporary object.
- A range-based for loop is used to iterate over the list:
Output:
Arjun (25 years old)