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)
Call
What 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().
Required. 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 Value
Description
Iterator
An 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;
}
Output
1 2 3 4 5
How It Works
d.begin() + 2 points at 4, the third element.
emplace(..., 3) builds a new element 3before that position.
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;
}
Output
(3, 4) (1, 2)
How It Works
Point has a constructor that takes two ints.
emplace_back(1, 2) builds the first point. Then emplace(pts.begin(), 3, 4) builds Point(3, 4) directly at the front.
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;
}
Output
inserted 20 at index 1
10 20 30
How It Works
emplace() builds 20 before the element at index 1 and returns an iterator to the new element.
*it reads the new value, and it - d.begin() gives its index, 1.
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.