Topics
Key differences:
- Construction Location
push_back()
creates the object first, then copies/moves it into the vectoremplace_back()
constructs the object directly in the vector’s memory
- Parameter Handling
push_back()
takes a complete object of the container’s typeemplace_back()
takes constructor arguments and forwards them to the object’s constructor
- Performance
emplace_back()
is generally more efficient as it avoids unnecessary copies/moves- Most noticeable with complex objects like strings or custom classes
class MyClass {
public:
int x;
std::string y;
public:
MyClass(int x, std::string y) : x(x), y(y) {}
};
std::vector<MyClass> vec;
vec.push_back(MyClass(42, "test")); // Creates temporary object, then moves it
vec.emplace_back(42, "test"); // Constructs object directly in vector}
cout << vec[0].x << endl;
Best practices:
- Use
emplace_back()
when constructing new objects - Use
push_back()
when adding existing objects push_back()
might be clearer when working with simple types like int or double