C++ std::list pop_front() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::list

Definition

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
CallWhat 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

ParameterDescription
Nonepop_front() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
Nothingpop_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;
}

How It Works

  1. The list holds four elements, and 10 is the first one.
  2. pop_front() unlinks the first node. The node with 20 becomes the new first node without being moved.
  3. 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.

C++
#include <iostream>
#include <list>
#include <string>

int main() {
    std::list<std::string> jobs;
    jobs.push_back("job1");
    jobs.push_back("job2");
    jobs.push_back("job3");

    while (!jobs.empty()) {
        std::cout << "run " << jobs.front() << '\n';
        jobs.pop_front();
    }
    return 0;
}

How It Works

  1. push_back() adds the jobs in the order job1, job2, job3.
  2. Each pass reads the first job with front() and then removes it with pop_front(). This is first in, first out.
  3. 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;
}

How It Works

  1. int taken = l.front(); copies 10 into a normal variable.
  2. pop_front() then destroys the first node. If we had only kept a reference, it would now be invalid.
  3. 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.

Next: push_back()

Add an element at the end of the list with std::list.

push_back() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful