In C++11 and later, std::list::emplace() creates a new element directly inside the list, right before the position you give. You pass the constructor arguments, not a finished object.
That avoids making a temporary object and then copying it. It is useful for strings, pairs, and your own classes. Include <list> to use it.
This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.
Syntax
Call emplace with an iterator, then the arguments for the element constructor.
C++
auto it = l.emplace(pos, 3, 'b'); // std::string(3, 'b') = "bbb"
l.emplace(l.end(), 1, "one"); // std::pair<int, std::string>
Call
What it does
l.emplace(pos, args...)
Builds a new element with T(args...) before pos. Returns an iterator to it.
insert() takes an existing object. emplace() takes the arguments to build one. For plain int they give the same result.
Library declarations
C++
template<class... Args>
iterator emplace(const_iterator pos, Args&&... args); // since C++11
Parameters
Parameter
Description
pos
Required. An iterator into this list. The new element is created before it. l.end() is allowed.
args...
The arguments that are forwarded to the constructor of the element type T.
Return Value
Return Value
Description
iterator
An iterator that points at the newly built element.
C++ Examples
Example 1 — emplace() a String from Arguments
Insert "bbb" without making a string first. The arguments 3, 'b' go straight to the std::string constructor.
C++
#include <iostream>
#include <iterator>
#include <list>
#include <string>
int main() {
std::list<std::string> l = {"a", "c"};
l.emplace(std::next(l.begin()), 3, 'b');
for (const std::string& s : l) {
std::cout << s << ' ';
}
std::cout << '\n';
return 0;
}
Output
a bbb c
How It Works
std::next(l.begin()) points at "c", so the new element goes before it.
emplace(pos, 3, 'b') passes 3 and 'b' to std::string(3, 'b'), which makes "bbb" inside the new node.
No temporary string was created and copied. The list now reads a bbb c.
Example 2 — emplace() a std::pair
Put items at both ends of a list of pairs by giving only the pieces of each pair.
The first call with l.end() builds the pair (1, "one") as the only element.
The second call with l.begin() builds (0, "zero") and links it before the first pair.
The loop reads p.first and p.second of every pair, so the two lines appear in list order.
Example 3 — emplace() Returns an Iterator
Use the returned iterator to read the new element or to insert next to it.
C++
#include <iostream>
#include <iterator>
#include <list>
int main() {
std::list<int> l = {1, 3};
auto it = l.emplace(std::next(l.begin()), 2);
std::cout << "built: " << *it << '\n';
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
built: 2
1 2 3
How It Works
The position is the node with 3, so the new element is linked between 1 and 3.
For an int, the single argument 2 is used as the value of the new element.
The returned iterator points at the new node, so *it prints 2.
Frequently Asked Questions
It builds a new element in place, before the position that you give, using the arguments that you pass to the constructor. It returns an iterator to the new element.
insert() receives an object that already exists and copies or moves it. emplace() receives constructor arguments and builds the object inside the list, so it can skip a copy.
C++11 and later. It was added together with move semantics and variadic templates.
Not always. For small types such as int there is no difference. It helps most with larger types such as strings and your own classes.
No. All existing iterators, pointers, and references stay valid after emplace() on a std::list.