C++ std::deque emplace_front() Function

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

Definition

In C++, std::deque::emplace_front() builds a new element at the front of the deque. You pass the constructor arguments, and the deque creates the object in place.

It was added in C++11. A std::vector has no such function, because adding at the front of a vector is slow. A deque does it quickly. 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 constructor arguments of the element.

C++
d.emplace_front(1);
pts.emplace_front(5, 6);    // calls Point(5, 6)
CallWhat it does
d.emplace_front(1)Builds a new element 1 before the current first element.
pts.emplace_front(5, 6)Calls the constructor Point(5, 6) directly at the front.

The matching back version is emplace_back(). The classic copy version is push_front().

Library declarations
C++
template<class... Args>
void      emplace_front(Args&&... args);     // C++11

template<class... Args>
reference emplace_front(Args&&... args);     // C++17

Parameters

ParameterDescription
args...Required. The arguments passed to the constructor of the element type T.

Return Value

Return ValueDescription
Reference (C++17)A reference to the new first element. In C++11 and C++14 the function returns void.

C++ Examples

Example 1 — emplace_front() with Numbers

Each new value goes in front of the older ones.

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

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

    d.emplace_front(2);
    d.emplace_front(1);

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

How It Works

  1. The deque starts with only 3.
  2. emplace_front(2) puts 2 before it, then emplace_front(1) puts 1 before that.
  3. The last call is the first element, so the printed order is 1 2 3.

Example 2 — emplace_front() with a Constructor

Let the deque build a Point from two numbers.

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

struct Point {
    int x, y;
    Point(int a, int b) : x(a), y(b) {}
};

int main() {
    std::deque<Point> pts;

    pts.emplace_front(1, 2);
    pts.emplace_front(5, 6);

    for (const Point& p : pts) {
        std::cout << "(" << p.x << ", " << p.y << ") ";
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. Point needs two ints, so emplace_front(1, 2) passes both to the constructor.
  2. The second call builds Point(5, 6) in front of the first point.
  3. The loop prints from front to back, so the newest point appears first.

Example 3 — Mix emplace_front() and emplace_back()

A deque can grow at both ends at the same time.

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

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

    words.emplace_front("Start");
    words.emplace_back("End");

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

How It Works

  1. The deque begins with one word in the middle.
  2. emplace_front("Start") adds a word before it, and emplace_back("End") adds one after it.
  3. Both ends are fast in a deque, which is the main reason to choose it over a std::vector.

Frequently Asked Questions

It builds a new element at the front of the deque from the constructor arguments you pass. The element is created in place.
push_front() takes an object that already exists and copies or moves it. emplace_front() takes constructor arguments and builds the object directly inside the deque.
No. Adding at the front of a vector would move every element, so the vector does not offer it. Use a deque or a list.
Since C++17 it returns a reference to the new element. In C++11 and C++14 it returns void.
Yes. It takes constant time (amortized) because a deque has room at the front. It does not move the old elements.

Next: empty()

Check whether the deque has no elements with std::deque.

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

10 people found this page helpful