Open In App

Vector emplace() vs insert() in C++

Last Updated : 12 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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():

Parametervector emplace()vector insert()
PurposeInsert 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 ObjectAvoids creating a temporary object, directly constructs the element in place.Requires an existing object, which is either already exists or created for insertion.
EfficiencyMore efficient for complex types since it avoids copies/moves.Less efficient with complex types due to extra moves/copies.
Syntaxv.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;
}

Output
3'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;
}

Output
3'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.


Next Article
Practice Tags :

Similar Reads