In C++, std::list::push_front() adds a new element at the start of a std::list. A new node is created and linked before the old first node.
Adding at the front is as fast as adding at the back, because a list does not need to shift anything. Include <list> to use it.
This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.
Syntax
Call push_front with the value to add. The value is copied, or moved when you pass a temporary.
C++
l.push_front(1); // copy a value
l.push_front(std::move(text)); // move a string in
Call
What it does
l.push_front(value)
Copies value into a new first node. The old first element becomes the second one.
l.push_front(std::move(value))
Moves value into the new first node.
If you push 1, then 2, then 3 to the front, the list reads 3 2 1. The newest element is always first.
Library declarations
C++
void push_front(const T& value);
void push_front(T&& value); // since C++11
Parameters
Parameter
Description
value
Required. The element to add at the front. It is copied, or moved if you pass an rvalue.
Return Value
Return Value
Description
Nothing
push_front() returns void. Use front() to read the new first element.
C++ Examples
Example 1 — push_front() Adds Before the First Element
Put 1 in front of an existing list.
C++
#include <iostream>
#include <list>
int main() {
std::list<int> l = {2, 3};
l.push_front(1);
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
std::cout << "front: " << l.front() << '\n';
return 0;
}
Output
1 2 3
front: 1
How It Works
The list holds 2 3. The first node is the one with 2.
push_front(1) links a new node in front of it. The nodes 2 and 3 do not move.
front() now reads the new first element, 1.
Example 2 — push_front() in a Loop Reverses the Order
Every new element goes to the front, so the last value pushed is the first one in the list.
C++
#include <iostream>
#include <list>
int main() {
std::list<int> l;
for (int i = 1; i <= 4; ++i) {
l.push_front(i);
}
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
4 3 2 1
How It Works
The loop pushes 1, then 2, then 3, then 4.
Each value goes to the front, so after the first push the list is 1, after the second 2 1, and so on.
The finished list is 4 3 2 1. Using push_back() in the same loop would give 1 2 3 4.
Example 3 — push_front() for a Recent-Items List
Keep only the three newest names. New names go to the front, and the oldest one is dropped from the back.
C++
#include <iostream>
#include <list>
#include <string>
int main() {
std::list<std::string> recent;
for (const std::string& s : {"a", "b", "c", "d"}) {
recent.push_front(s);
if (recent.size() > 3) {
recent.pop_back();
}
}
for (const std::string& s : recent) {
std::cout << s << ' ';
}
std::cout << '\n';
return 0;
}
Output
d c b
How It Works
Each new name is added to the front, so the newest name is always first.
When the list grows to four names, pop_back() removes the last one, which is the oldest. After "d" arrives, "a" is dropped.
The list ends as d c b: the three newest names, newest first. Both ends of a list are fast, so this stays cheap.
Frequently Asked Questions
It adds a new element at the beginning of the list. A new node is created and linked before the old first node.
A vector keeps elements in one block, so adding at the front means moving all other elements one place. That would be slow, so the vector does not offer it.
No. It is as fast as push_back(), because only a few links change and no element is moved.
No. All existing iterators, pointers, and references stay valid after push_front() on a std::list.
They give the same result. push_front() is shorter and easier to read, while insert() also returns an iterator to the new element.