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.
Required. The arguments passed to the constructor of the element type T.
Return Value
Return Value
Description
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;
}
Output
1 2 3
How It Works
The deque starts with only 3.
emplace_front(2) puts 2 before it, then emplace_front(1) puts 1 before that.
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;
}
Output
(5, 6) (1, 2)
How It Works
Point needs two ints, so emplace_front(1, 2) passes both to the constructor.
The second call builds Point(5, 6) in front of the first point.
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;
}
Output
Start middle End
How It Works
The deque begins with one word in the middle.
emplace_front("Start") adds a word before it, and emplace_back("End") adds one after it.
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.