push_back vs emplace_back
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In modern C++ programming, efficiently adding elements to containers such as std::vector is crucial for performance-sensitive applications. Among the member functions that std::vector provides for inserting elements at the end of the container are push_back and emplace_back. Both perform similar tasks but in slightly different ways, each having implications on performance and usage. Understanding the differences between these two functions is valuable for writing optimal C++ code.
Understanding push_back
The push_back member function inserts a new element at the end of the vector, which is constructed from the provided argument. Essentially, push_back takes a single parameter of the type stored in the vector (or a type convertible to the type stored in the vector) and copies or moves it into the new element's location.
Let's see an example:
In this example, push_back is called twice – first with a named object, which results in copying, and then with a temporary object, which results in moving.
Understanding emplace_back
Introduced in C++11, emplace_back attempts to optimize the process of constructing elements within containers by eliminating unnecessary copy or move operations. Unlike push_back, which constructs an object and then moves or copies it into the vector, emplace_back constructs the object directly in the location it will occupy in the vector.
Here’s how you might use emplace_back:
Here, the string "Hello" is constructed exactly where it needs to reside within the vector. This direct construction can lead to performance benefits, especially when dealing with complex data types.
Performance Comparison
The primary advantage of emplace_back over push_back is that it can construct objects in-place. This is particularly effective if the constructor of the object being added is expensive or if the object does not support moving efficiently but does support constructing with parameters directly.
Consider when a class has an expensive copy constructor or no move constructor defined, and the class has a constructor that accepts parameters for direct initialization:
Here, emplace_back avoids the extra copy, constructing the BigObject directly within the vector’s storage.
Summary Table
| Function | Description | Use Case |
push_back | Copies or moves the argument into the vector. | Use when you already have an object. |
emplace_back | Constructs an object directly in the location within the container. | Use when constructing an object from its arguments. |
Conclusion
While push_back is suitable for adding elements that are already constructed, emplace_back provides a more efficient approach by constructing elements in place when possible. This makes emplace_back generally preferable when working with types that have expensive move or copy constructors or when the object can be constructed from arguments directly.
Choosing between these two functions ultimately depends on the specific use case and efficiency requirements of your application. When in doubt, profiling and measuring performance differences in a real-world context can provide insights into the best approach for your particular scenario.
Related reading
- Push_swap sorting 50000 numbers with two rotatable stacks and a limited set of operations
- Python- What word can have the most consecutive letters removed and still be a dictionary-valid word?
- Python - Count elements in list
- Python - Exit Kafka queue once all messages have been read
- Python vs C Tensorflow inferencing
- Quicksort weird time complexity, c
- Python - Is a dictionary slow to find frequency of each character?
- Python - Tree traversal question

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.