C++ std::deque emplace_back() Function

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

Definition

In C++, std::deque::emplace_back() builds a new element at the back of the deque. You give it the constructor arguments, and the deque creates the object in place.

It was added in C++11. It is often used instead of push_back() when the element type needs several arguments to be built. 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_back(5);
names.emplace_back(3, 'a');     // std::string(3, 'a')
auto& ref = d.emplace_back(7);  // C++17
CallWhat it does
d.emplace_back(5)Builds a new element 5 after the current last element.
names.emplace_back(3, 'a')Calls the string constructor with (3, 'a') and stores the string "aaa".
auto& ref = d.emplace_back(7)Since C++17 the function returns a reference to the new element.

The matching front version is emplace_front(). The classic copy version is push_back().

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

template<class... Args>
reference emplace_back(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 element. In C++11 and C++14 the function returns void.

C++ Examples

Example 1 — emplace_back() with Numbers

For simple types it works like push_back().

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

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

    d.emplace_back(3);
    d.emplace_back(4);

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

How It Works

  1. The deque starts as 1 2.
  2. Each emplace_back() builds one new element at the back end.
  3. The final order is 1 2 3 4, because later calls go further to the back.

Example 2 — emplace_back() with String Arguments

Pass the arguments of the std::string constructor instead of a ready string.

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

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

    names.emplace_back(3, 'a');
    names.emplace_back("Mia");

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

How It Works

  1. emplace_back(3, 'a') forwards 3 and 'a' to the string constructor, which makes "aaa".
  2. emplace_back("Mia") builds a string from the text directly inside the deque.
  3. push_back(3, 'a') would not compile, because push_back() needs one finished object.

Example 3 — Use the Reference from emplace_back()

Since C++17 you can change the new element right away.

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

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

    auto& ref = d.emplace_back(5);
    ref += 10;

    std::cout << "back: " << d.back() << '\n';
    return 0;
}

How It Works

  1. emplace_back(5) adds 5 and returns a reference to it. Compile with C++17 or newer.
  2. ref += 10 changes the element inside the deque, because ref is a reference and not a copy.
  3. back() now reads 15, which proves that the deque element was modified.

Frequently Asked Questions

It builds a new element at the back of the deque from the constructor arguments you pass. The element is created in place, so no extra copy is needed.
push_back() takes an object that already exists and copies or moves it. emplace_back() takes constructor arguments and builds the object inside the deque.
Since C++17 it returns a reference to the new element. In C++11 and C++14 it returns void.
Not always. For simple types such as int there is no difference. It helps most with bigger objects that have a costly copy.
Yes, with the same syntax. A deque can also add at the front with emplace_front(), which a vector cannot do.

Next: emplace_front()

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

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

9 people found this page helpful