
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
emplace vs insert in C++ STL
In C++ STL, both emplace and insert functions are used to add elements to containers such as vectors, lists, and maps. But, emplace is more efficient than insert as it avoids unnecessary copying of object and does the insertion more efficiently than insert operation. In this article, we will discuss the all the differences between emplace and insert with examples.
Insert in C++ STL
The insert function in C++ STL is used to add new elements to a container (ie, set, map, vector, list, etc.). Following is the syntax of insert function:
// insert into a pair multiset<pair> set1; set1.insert(make_pair(1, "Alice")); // insert into a vector vector<int> vec1; vec1.insert(vec1.begin(), 10); // insert 10 at the beginning
In the above code, we have used the insert function to add elements to a multiset and a vector. Note that to insert a pair into a multiset, we have to make a pair using make_pair function. This will create a copy of the pair and insert it into the multiset.
Example
In the code below, we will create a multiset and a vector, and we will insert elements into them using the insert function.
#include <iostream> #include <set> #include <vector> using namespace std; int main() { // Create a multiset of pairs multiset<pair<int, string>> set1; // Insert a pair into the multiset set1.insert(make_pair(1, "Alice")); set1.insert(make_pair(2, "Bob")); // Display the elements in the multiset cout << "Multiset elements:" << endl; for (const auto& p : set1) { cout << p.first << ": " << p.second << endl; } // Create a vector vector<int> vec1; // Insert an element at the beginning of the vector vec1.insert(vec1.begin(), 10); vec1.insert(vec1.end(), 20); // Insert at the end // Display the elements in the vector cout << "Vector elements:" << endl; for (const auto& v : vec1) { cout << v << " "; } cout << endl; return 0; }
The output of the above code will be:
Multiset elements: 1: Alice 2: Bob Vector elements: 10 20
Emplace in C++ STL
The emplace function in C++ STL is used insert new elements into a container without creating a copy of the object. Meaning, it constructs the object in place. Hence, this is more efficient than the insert function. The syntax of emplace function is given below:
// Using emplace to insert into a multiset multiset<pair<int, string>> set1; set1.emplace(1, "Alice"); set1.emplace(2, "Bob"); // Using emplace to insert into a vector vector<int> vec1; vec1.emplace(vec1.begin(), 10); // insert 10 at the beginning
Now, you can understand the difference between insert and emplace. When you are using emplace, you do not need to create a pair (or any object) to insert into the container. You can directly pass the arguments to the emplace function, and it will construct the object in place.
Example
In the code below, we will create a multiset and a vector, and we will insert elements into them using the emplace function.
#include <iostream> #include <set> #include <vector> using namespace std; int main() { // Create a multiset of pairs multiset<pair<int, string>> set1; // Emplace a pair into the multiset set1.emplace(1, "Alice"); set1.emplace(2, "Bob"); // Display the elements in the multiset cout << "Multiset elements:" << endl; for (const auto& p : set1) { cout << p.first << ": " << p.second << endl; } // Create a vector vector<int> vec1; // Emplace an element at the beginning of the vector vec1.emplace(vec1.begin(), 10); vec1.emplace(vec1.end(), 20); // Emplace at the end // Display the elements in the vector cout << "Vector elements:" << endl; for (const auto& v : vec1) { cout << v << " "; } cout << endl; return 0; }
The output of the above code will be:
Multiset elements: 1: Alice 2: Bob Vector elements: 10 20
Difference between Emplace and Insert
Here are the key differences between emplace and insert in C++ STL:
Criteria | Emplace | Insert |
---|---|---|
Definition | The emplace function is used to insert new elements into a container by constructing the object in place. | The insert function is used to add new elements to a container by copying or moving the object. |
Accepts Arguments | Accepts the arguments required to construct the object directly. | Insert function accepts only a pre constructed object. |
Efficiency | More efficient as it avoids unnecessary copying of the object. | Less efficient as it may involve copying or moving the object. |
C++ Version | Available only in C++11 and later. | Insert function available in all C++ versions. |
Example |
set1.emplace(3, "Charlie"); |
set1.insert(make_pair(3, "Charlie")); |