C++ std::deque pop_front() Function

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

Definition

In C++, std::deque::pop_front() removes the first element of a std::deque. The size becomes one smaller, and the second element becomes the new first element.

This is one of the main reasons to use a deque. A std::vector has no pop_front(), because removing the first element of a vector moves every other element. Include <deque> to use it.

This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.

Syntax

Call pop_front with empty brackets. It takes no arguments.

C++
d.pop_front();
CallWhat it does
d.pop_front()Removes the first element and makes the deque one element shorter.

The back version is pop_back(). To add at the front, use push_front().

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 value is destroyed.

C++ Examples

Example 1 — Basic pop_front()

Remove the first number and print what is left.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {1, 2, 3};

    d.pop_front();

    for (int n : d) {
        std::cout << n << ' ';
    }
    std::cout << "(front is now " << d.front() << ")\n";
    return 0;
}

How It Works

  1. The deque starts as 1 2 3, with 1 at the front.
  2. pop_front() removes 1. The other elements do not need to be moved.
  3. 2 is now the first element, which front() confirms.

Example 2 — Remove Values While a Condition Is True

In a sorted deque, drop all the small values from the front.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {1, 3, 5, 12, 20};

    while (!d.empty() && d.front() < 10) {
        d.pop_front();
    }

    for (int n : d) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The loop checks !d.empty() first. Then it checks whether the front value is below 10.
  2. The values 1, 3, and 5 are each at the front in turn, and each one is removed by pop_front().
  3. When 12 becomes the front, the condition is false and the loop stops. 12 20 remain.

Example 3 — Read with front(), Then Remove

pop_front() returns nothing, so keep the value before you remove it.

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

int main() {
    std::deque<std::string> tickets = {"A1", "A2", "A3"};

    std::string served = tickets.front();
    tickets.pop_front();

    std::cout << "served: " << served << '\n';
    std::cout << "waiting: " << tickets.size() << '\n';
    return 0;
}

How It Works

  1. tickets.front() reads "A1", and the copy is stored in served.
  2. pop_front() removes the ticket from the deque. The copy in served stays safe.
  3. Two tickets are still waiting, so size() prints 2.

Frequently Asked Questions

It removes the first element of the deque. The second element becomes the new first element.
No. Removing the first element of a vector would move all other elements, so the vector does not offer it. A deque does it quickly.
No. It returns void. Read the value with front() before you call pop_front().
It is undefined behavior. Check empty() first.
Yes. It takes constant time, because no other element has to move.

Next: push_back()

Add an element at the back with std::deque.

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