In C++, std::vector::emplace_back() builds a new element at the back of the vector. You give it the constructor arguments, and the vector 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 <vector> to use it.
This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.
emplace_back(5) adds 5 and returns a reference to it. Compile with C++17 or newer.
ref += 10 changes the element inside the vector, because ref is a reference and not a copy.
back() now reads 15, which proves that the vector element was modified.
Frequently Asked Questions
It builds a new element at the back of the vector 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 vector.
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 vector can also add at the front with emplace_front(), which a vector cannot do.