In C++, std::list::pop_front() removes the first element of a std::list. The first node is unlinked and destroyed, and the second node becomes the new first one.
Like pop_back(), it returns nothing. Read the value with front() before you pop. 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 pop_front with empty brackets. Check that the list is not empty first.
C++
int first = l.front(); // read first
l.pop_front(); // then remove it
Call
What it does
l.pop_front()
Removes the first element. size() becomes one smaller.
With push_back() and pop_front() a list works like a queue: first in, first out.
Library declarations
C++
void pop_front();
Parameters
Parameter
Description
None
pop_front() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Nothing
pop_front() returns void. The removed element is destroyed.
C++ Examples
Example 1 — pop_front() Removes the First Element
Take the first number off a list of four.
C++
#include <iostream>
#include <list>
int main() {
std::list<int> l = {10, 20, 30, 40};
l.pop_front();
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
std::cout << "new front: " << l.front() << '\n';
return 0;
}
Output
20 30 40
new front: 20
How It Works
The list holds four elements, and 10 is the first one.
pop_front() unlinks the first node. The node with 20 becomes the new first node without being moved.
The loop prints the remaining three values, and front() now returns 20.
Example 2 — pop_front() to Process a Queue
Add jobs at the back and take them from the front. The oldest job always comes out first.
push_back() adds the jobs in the order job1, job2, job3.
Each pass reads the first job with front() and then removes it with pop_front(). This is first in, first out.
The loop ends when empty() is true, so pop_front() is never called on an empty list.
Example 3 — Save the Value Before pop_front()
Because pop_front() returns nothing, copy the first element into a variable before you remove it.
C++
#include <iostream>
#include <list>
int main() {
std::list<int> l = {10, 20, 30};
int taken = l.front();
l.pop_front();
std::cout << "took " << taken << ", now first is " << l.front() << '\n';
std::cout << "size: " << l.size() << '\n';
return 0;
}
Output
took 10, now first is 20
size: 2
How It Works
int taken = l.front(); copies 10 into a normal variable.
pop_front() then destroys the first node. If we had only kept a reference, it would now be invalid.
The variable still holds 10, the list starts with 20, and size() is 2.
Frequently Asked Questions
It removes the first element of the list and destroys it. The next element becomes the new first element. It returns nothing.
No. It returns void. Use front() to read the value before you call pop_front().
The behavior is undefined. Check empty() before you call it.
Yes. Only a few links change, and no other element moves. This is different from std::vector, which has no pop_front() because removing the first element is slow.
Yes. Use push_back() to add and pop_front() to remove. The standard std::queue adapter can also use a list or a deque inside.