Open In App

vector emplace() in C++ STL

Last Updated : 16 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 5, 8};
  
  	// Inserting 6 at index 2
    v.emplace(v.begin() + 2, 6);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 5 6 8 

This article covers the syntax, usage, and common queries of vector emplace() method in C++:

Syntax of vector emplace()

The vector emplace() is the member method of std::vector defined inside <vector> header file.

v.emplace(pos, args...);

Parameters

  • pos: Iterator to the position where element is to be inserted.
  • args...: Arguments forwarded to the constructor of the vector's element type.

Return Value

  • Returns an iterator to the inserted element.

Examples of vector emplace()

The following examples demonstrates the use of vector emplace() function for different insertion position and number of elements:

Insert an Element at the End of Vector

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 4, 5};

    // Insert element 6 at the end
    v.emplace(v.end(), 6);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 4 5 6 

We can also use the vector emplace_back() method for insert an element at the end of vector.

Insert Element at Specific Position in a Vector of Custom Data Type

C++
#include <bits/stdc++.h>
using namespace std;

// Custom Type
class A {
  public:
  int a, b, c;
  A(int x = 0, int y = 0, int z = 0):
    a(x), b(y), c(z) {}
};

int main() {
    vector<A> v = {{1, 4, 5}};

    // Insert an element at index 1
    v.emplace(v.begin() + 1, 2, 3, 6);

    for (auto i : v)
        cout << i.a << " " << i.b << " "
      		<< i.c << endl;
    return 0;
}

Output
1 4 5
2 3 6

Explanation: The argument we pass to the vector emplace() method are forwarded to the constructor of the vector's element type. So, the arguments 2, 3, and 6 are passed to the constructor of A which then creates an object in-place to insert it into the vector.

Difference Between vector emplace() and vector insert()

Both vector emplace() and vector insert() are used to insert elements into the vector, but they differ from each other in how they operate. The following table lists the primary differences between the vector emplace and vector insert():

Featurevector emplace()vector insert()
PurposeInsert a single element in the vector at given position.Insert a single or multiple elements 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)

Next Article
Practice Tags :

Similar Reads