Push_back vs Emplace_back in C++: Key Differences and Usage
push_back adds a copy or move of an existing object to a container, while emplace_back constructs the object in place directly inside the container using provided arguments. emplace_back can be more efficient by avoiding unnecessary copies or moves.Quick Comparison
Here is a quick side-by-side comparison of push_back and emplace_back in C++.
| Factor | push_back | emplace_back |
|---|---|---|
| Operation | Copies or moves an existing object into the container | Constructs the object directly inside the container |
| Arguments | Takes an object (copy or move) | Takes constructor arguments for the object |
| Performance | May involve extra copy or move operations | Avoids extra copies or moves, more efficient |
| Use case | When you already have a ready object | When you want to create the object in place |
| Introduced in | C++98 | C++11 |
| Syntax complexity | Simpler for existing objects | More flexible for direct construction |
Key Differences
push_back requires you to create an object first and then adds a copy or move of that object to the container. This means the object is constructed outside the container and then copied or moved inside, which can cause extra overhead.
On the other hand, emplace_back constructs the object directly inside the container using the arguments you provide. This avoids unnecessary copying or moving, making it more efficient especially for complex objects.
Because emplace_back forwards the arguments to the constructor of the contained type, it allows you to create objects with any constructor parameters right inside the container, which push_back cannot do directly.
Code Comparison
Using push_back to add an object to a std::vector:
#include <iostream>
#include <vector>
#include <string>
struct Person {
std::string name;
int age;
Person(std::string n, int a) : name(std::move(n)), age(a) {
std::cout << "Person constructed\n";
}
};
int main() {
std::vector<Person> people;
Person p("Alice", 30); // Construct object first
people.push_back(p); // Copy into vector
people.push_back(Person("Bob", 25)); // Move into vector
return 0;
}emplace_back Equivalent
Using emplace_back to construct objects directly inside the std::vector:
#include <iostream>
#include <vector>
#include <string>
struct Person {
std::string name;
int age;
Person(std::string n, int a) : name(std::move(n)), age(a) {
std::cout << "Person constructed\n";
}
};
int main() {
std::vector<Person> people;
people.emplace_back("Alice", 30); // Construct in place
people.emplace_back("Bob", 25); // Construct in place
return 0;
}When to Use Which
Choose push_back when you already have a fully constructed object that you want to add to the container. It is simple and clear in this case.
Choose emplace_back when you want to create the object directly inside the container to avoid extra copies or moves, especially for complex objects or when you want to pass constructor arguments directly.
In modern C++, prefer emplace_back for better performance and flexibility unless you specifically need to add an existing object.
Key Takeaways
emplace_back constructs objects in place, avoiding extra copies or moves.push_back adds a copy or move of an existing object to the container.emplace_back for efficiency and direct construction with arguments.push_back when you already have a ready object to add.emplace_back was introduced in C++11 and is preferred in modern code.