C++ std::deque emplace() Function

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

Definition

In C++, std::deque::emplace() creates a new element inside the deque at a position you choose. You pass the arguments for the element constructor, not a ready-made object, so no extra copy is needed.

It was added in C++11. It is the in-place version of insert(). 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 an iterator for the position, then the constructor arguments.

C++
auto it = d.emplace(d.begin() + 2, 99);
pts.emplace(pts.begin(), 3, 4);   // calls Point(3, 4)
CallWhat it does
d.emplace(pos, value)Builds one new element before pos. The new element is made from value.
pts.emplace(pos, 3, 4)Calls the constructor Point(3, 4) directly inside the deque.

For the two ends there are faster shortcuts: emplace_front() and emplace_back().

Library declarations
C++
template<class... Args>
iterator emplace(const_iterator pos, Args&&... args);

Parameters

ParameterDescription
posRequired. An iterator. The new element is placed before this position. d.end() adds at the back.
args...Required. The arguments passed to the constructor of the element type T.

Return Value

Return ValueDescription
IteratorAn iterator that points to the new element.

C++ Examples

Example 1 — emplace() a Number in the Middle

Put 3 between 2 and 4.

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

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

    d.emplace(d.begin() + 2, 3);

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

How It Works

  1. d.begin() + 2 points at 4, the third element.
  2. emplace(..., 3) builds a new element 3 before that position.
  3. The elements 4 and 5 shift one place back to make room, so the order is 1 2 3 4 5.

Example 2 — emplace() with a Constructor

Pass the constructor arguments and let the deque build the object.

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_back(1, 2);
    pts.emplace(pts.begin(), 3, 4);

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

How It Works

  1. Point has a constructor that takes two ints.
  2. emplace_back(1, 2) builds the first point. Then emplace(pts.begin(), 3, 4) builds Point(3, 4) directly at the front.
  3. No temporary Point is created and copied. The result prints the front point first.

Example 3 — Use the Iterator Returned by emplace()

The return value tells you where the new element lives.

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

int main() {
    std::deque<int> d = {10, 30};

    auto it = d.emplace(d.begin() + 1, 20);

    std::cout << "inserted " << *it << " at index " << (it - d.begin()) << '\n';
    for (int n : d) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. emplace() builds 20 before the element at index 1 and returns an iterator to the new element.
  2. *it reads the new value, and it - d.begin() gives its index, 1.
  3. The older iterators may be invalid after the insert, which is why the code uses the returned iterator it instead.

Frequently Asked Questions

It builds a new element directly inside the deque at a given position, using the constructor arguments you pass. It returns an iterator to the new element.
insert() takes a finished object and copies or moves it in. emplace() takes constructor arguments and builds the object in place, which can save a copy.
C++11. Any modern compiler supports it.
No. Elements after the position must move, so it takes linear time. It is only fast near the two ends.
Yes, with the same syntax. A deque can additionally emplace at the front quickly with emplace_front(), which a vector does not have.

Next: emplace_back()

Build a new element in place at the back (C++11) with std::deque.

emplace_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.

8 people found this page helpful