C++ std::list emplace() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::list

Definition

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>
CallWhat 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

ParameterDescription
posRequired. 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 ValueDescription
iteratorAn 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;
}

How It Works

  1. std::next(l.begin()) points at "c", so the new element goes before it.
  2. emplace(pos, 3, 'b') passes 3 and 'b' to std::string(3, 'b'), which makes "bbb" inside the new node.
  3. 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.

C++
#include <iostream>
#include <list>
#include <string>
#include <utility>

int main() {
    std::list<std::pair<int, std::string>> l;

    l.emplace(l.end(), 1, "one");
    l.emplace(l.begin(), 0, "zero");

    for (const auto& p : l) {
        std::cout << p.first << " " << p.second << '\n';
    }
    return 0;
}

How It Works

  1. The first call with l.end() builds the pair (1, "one") as the only element.
  2. The second call with l.begin() builds (0, "zero") and links it before the first pair.
  3. 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;
}

How It Works

  1. The position is the node with 3, so the new element is linked between 1 and 3.
  2. For an int, the single argument 2 is used as the value of the new element.
  3. 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.

Next: emplace_back()

Build a new last element in place (C++11) with std::list.

emplace_back() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful