In my thought, emplace
is always a better option than insert
, because in some case emplace
could save one time of ctor and in other cases it could directly replace insert
with the same invoking form.
But now I have a map and add an element into it with insert
with initialize_list
map<string, map<int,int>> m1;
if (auto it = m1.find("c1"); it == m1.end()){
m1.insert({ "c1", {{10, 1}} });
}
But with emplace
I could not think of any way that as consice as insert
and still get the benifits what emplace
provides.Like below? It would be ill-formed and compiles in error.
auto [it, inserted] = m1.try_emplace("c1", 10, 1);
So how to emplace
in my case? And is it true that emplace
is always better than insert
?