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.
emplace_back(5) adds 5 and returns a reference to it. Compile with C++17 or newer.
ref += 10 changes the element inside the deque, because ref is a reference and not a copy.
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.