Vector emplace() vs insert() in C++
Last Updated :
12 Nov, 2024
In C++, STL vector provides two methods for element insertion at the given position: vector emplace() and vector insert(). Although both methods provide similar functionality, there are a difference regarding how they work.
The following table lists the primary differences between the vector emplace and vector insert():
Parameter | vector emplace() | vector insert() |
---|
Purpose | Insert a single element in the vector at given position in-place. | Insert a single, multiple elements or even elements from other containers in a vector at given position. |
Temporary Object | Avoids creating a temporary object, directly constructs the element in place. | Requires an existing object, which is either already exists or created for insertion. |
Efficiency | More efficient for complex types since it avoids copies/moves. | Less efficient with complex types due to extra moves/copies. |
Syntax | v.emplace(pos, args...) | v. insert ( pos, val ) v.insert(pos, n, val) v.insert(pos, first, last) |
Examples to Demonstrate the Difference
The in-place insertion of the vector emplace() can be verified in the below program:
C++
#include <bits/stdc++.h>
using namespace std;
// Class that signals when its constructor
// or copy constructor is called.
class A {
public:
int a;
A (int x = 0): a(x) {
cout << x << "'s C called\n";
}
A (const A& other) {
cout << other.a << "'s CC called\n";
this->a = other.a;
}
};
int main() {
vector<A> v;
// Reserve size to prevent reallocation
v.reserve(10);
// Insert an element using vector()
v.insert(v.end(), 3);
// Insert an element using emplace()
v.emplace(v.end(), 6);
return 0;
}
Output3's C called
3's CC called
6's C called
Explanation:
- We inserted 3 in the vector using vector insert() function. This created 2 constructor calls: first to the parameterized to construct the temporary object with value 3. Next call is to copy constructor for copying this object to vector.
- The other element 6 is inserted using vector emplace() and we can see that only the parameterized constructor is called.
In the above program, we have directly inserted the element using value, but what happens if we try to insert an already existing element.
Inserting an Already Existing Element
Let's see what happens if we try to insert an element that already exists in the vector.
C++
#include <bits/stdc++.h>
using namespace std;
// Class that signals when its constructor
// or copy constructor is called.
class A {
public:
int a;
A (int x = 0): a(x) {
cout << x << "'s C called\n";
}
A (const A& other) {
cout << other.a << "'s CC called\n";
this->a = other.a;
}
};
int main() {
vector<A> v;
v.reserve(10);
A x(3);
A y(6);
// Insert an element using insert()
v.insert(v.end(), x);
// Insert an element using emplace()
v.emplace(v.end(), y);
return 0;
}
Output3's C called
6's C called
3's CC called
6's CC called
Explanation: As we can see, there is no difference left when we try to insert an already existing element as vector emplace() function will also have to copy the element into the vector (as it is already constructed from a value beforehand). The same will be true for vector insert(). As the element is already constructed, it just have to create a copy of it inside vector.
Similar Reads
emplace vs insert in C++ STL In C++, all containers (vector, stack, queue, set, map, etc) support both insert and emplace operations. Both are used to add an element in the container.The advantage of emplace is, it does in-place insertion and avoids an unnecessary copy of object. For primitive data types, it does not matter whi
1 min read
vector emplace() in C++ STL In C++, vector emplace() is used to insert elements at the given position in a vector by constructing it in-place within the vector, rather than creating a temporary object and then moving or copying it into the vector.Let's take a quick look at a simple example that uses vector emplace() method:C++
3 min read
Vector emplace_back in C++ STL In C++, the vector emplace_back() is a built-in method used to insert an element at the end of the vector by constructing it in-place. It means that the element is created directly in the memory allocated to the vector avoiding unnecessary copies or moves.Letâs take a quick look at a simple example
3 min read
set::emplace() in C++ STL Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::emplace() This f
4 min read
Vector size vs capacity in C++ STL In C++, the size of a vector is the number of elements in the vector. It can grow or shrink as elements are added or removed. To store these elements, each vector is allocated some memory. This total allocated memory is called the capacity of the vector.The following table lists the primary differen
3 min read