In C++17, std::map and std::unordered_map got a new member-function template: try_emplace(). This new addition, proposed in n4279, behaves similarly to emplace(), but has th
I would always prefer try_emplace over emplace. A Crucial difference is that try_emplace 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
For example the below code (Example from https://github.com/PacktPublishing/Cpp17-STL-Cookbook/blob/master/Chapter02/efficient_insert_or_reassign_to_map.cpp)
#include
#include
#include
#include
For the above code
m.try_emplace(b.country, b, 1);
if there unsuccessful insertion the pairs will not get constructed which adds to the performance