In this article, we are going to learn try_emplace method in Maps and Unordered Maps. This method was added in C++17 (i.e gcc 9.1) version. This new function proposed behaves similarly to emplace(), but has an advantage that is, it will not construct the object associated with the key, if the key already exists. This will boost the performance in case objects of that type are expensive to create.
Parameters: The function accepts two mandatory parameters which are described below:
- key: It specifies the key to be inserted in the multimap container.
- element: It specifies the element to the key which is to be inserted in the map container.
Return Value: The function does not return anything.
Below is the program to illustrate try_emplace() in C++:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live