push_back() vs emplace_back() in C++ STL Containers

Cengizhan Varlı
4 min readJul 8, 2023

--

In C++, push_back and emplace_back are member functions of container classes such as std::vector, std::deque, and std::list. They are used to add elements to the end of the container.

push_back()

It is a member function that adds a copy or a moved version of an object to the back of the container. It takes the object as a parameter and creates a copy or moves it into the container. The container needs to make a copy or perform a move operation, which may involve memory allocation and copying of data.

Output

An object of myClass named “m” is created using the default constructor. The message "Ctor" is printed to the console.

The push_back function is called on the “vec” vector, passing “m” as the argument. This will invoke the copy constructor of myClass since "m" is being copied into the vector. The message "Copy Ctor" is printed to the console.

As you can see, when adding with vector push_back(), insertion is made with copy semantics.

So when we add a class to the vector, the class has to be copiable. Because it adds to the vector by copying.

What if the related class is closed to copying ?

As you can see an error has occurred. Because the copy ctor has been deleted.

In modern C++, it is possible to add to the vector without copying.

emplace_back()

It is a member function that constructs the object in place at the end of the container, directly calling the object's constructor with the provided arguments. Instead of creating a separate object and then copying or moving it, emplace_back constructs the object directly in the container's memory space, avoiding unnecessary copies or moves.

The emplace_back function is called on the "vec" vector, passing "10” as the argument. This will construct a new object of myClass directly within the vector using the parameterized constructor. The message "Parameterized Ctor" is printed to the console.

The key difference between push_back and emplace_back is that push_back requires an object to be already created (or moved) before it can be added to the container, while emplace_back directly constructs the object within the container itself.

In terms of performance, emplace_back generally has an advantage over push_back when constructing objects in a container like std::vector or std::deque. Here's why:

  1. Construction Efficiency: emplace_back constructs objects directly in-place within the container, forwarding the constructor arguments. It avoids the need for creating temporary objects or performing unnecessary copies or moves. This can lead to improved performance, especially when dealing with expensive-to-copy or large objects.
  2. Memory Allocation: With emplace_back, the container can optimize memory allocation by reserving enough space in advance, considering the size requirements of the constructed object. This can reduce the number of reallocations and improve performance compared to repeated push_back calls.
  3. Exception Safety: emplace_back provides strong exception safety. If an exception occurs during object construction, the container remains unchanged and no partial objects are left in an inconsistent state. In contrast, push_back may involve temporary object creation, and if an exception occurs during copying or moving, the container may end up in an inconsistent state.

The choice between emplace_back and push_back depends on the specific requirements of your code and the type of objects being inserted. If constructing objects in-place and avoiding unnecessary copies or moves is important for performance, emplace_back is typically the preferred choice.

--

--

No responses yet