C++ std::deque push_front() Function

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

Definition

In C++, std::deque::push_front() adds a new element at the front of a std::deque. The size becomes one larger, and the new element becomes the first one.

This is the main thing a deque can do that a std::vector cannot do quickly. A vector has no push_front(), because every element would have to move. 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

Pass the value to add. There are two forms: copy and move.

C++
d.push_front(10);
words.push_front(std::string("hello"));
CallWhat it does
d.push_front(10)Adds a copy of 10 before the current first element.
words.push_front(std::string("hello"))Adds a temporary object. It is moved into the deque instead of being copied.

The matching back version is push_back(). For constructor arguments, use emplace_front().

Library declarations
C++
void push_front(const T& value);
void push_front(T&& value);   // C++11

Parameters

ParameterDescription
valueRequired. The value to add. A normal value is copied, and a temporary is moved into the deque.

Return Value

Return ValueDescription
Nothingpush_front() returns void. Use front() if you want to see the new element.

C++ Examples

Example 1 — Basic push_front()

Add a word in front of an existing word.

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

int main() {
    std::deque<std::string> words = {"world"};

    words.push_front("hello");

    for (const std::string& w : words) {
        std::cout << w << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The deque starts with one word, "world".
  2. push_front("hello") places "hello" before it, so it becomes the first element.
  3. The loop prints from front to back: hello world.

Example 2 — push_front() and push_back() Together

Grow the deque from both ends and watch the order.

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

int main() {
    std::deque<int> d;

    d.push_back(2);
    d.push_front(1);
    d.push_back(3);
    d.push_front(0);

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

How It Works

  1. push_back(2) adds the first element, so the deque is 2.
  2. push_front(1) gives 1 2, push_back(3) gives 1 2 3, and push_front(0) gives 0 1 2 3.
  3. Both ends are fast. A vector can only add quickly at the back.

Example 3 — Keep the Newest Item First

Use push_front() so the most recent message is always at the front.

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

int main() {
    std::deque<std::string> log;

    log.push_front("boot");
    log.push_front("login");
    log.push_front("logout");

    for (const std::string& line : log) {
        std::cout << line << '\n';
    }
    return 0;
}

How It Works

  1. The messages are pushed in time order: boot, then login, then logout.
  2. Each push_front() places the new message before all older ones.
  3. Reading from the front gives the newest message first. With push_back() the order would be the opposite.

Frequently Asked Questions

It adds a new element at the front of the deque. The new element becomes the first one, and the size grows by one.
No. Adding at the front of a vector would move every element, which is slow. Use a deque when you need fast adds at the front.
Yes. It takes constant time (amortized), because the deque keeps free space at the front.
Each new element goes before all older ones. The last element you push is the first element in the deque.
Both add at the front. push_front() is shorter and clearer. insert(begin(), x) also returns an iterator.

Next: rbegin()

Get a reverse iterator to the last element with std::deque.

rbegin() 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.

13 people found this page helpful