There are two ways of map insertion:
m[key] = val;
Or
m.insert(make_pair(key, val));
My question is, which op
My answer is not on efficiency but on safety, which is relevant to choosing an insertion algorithm:
The []
and insert()
calls would trigger destructors of the elements. This may have dangerous side effects if, say, your destructors have critical behaviors inside.
After such a hazard, I stopped relying on STL's implicit lazy insertion features and always use explicit checks if my objects have behaviors in their ctors/dtors.
See this question: Destructor called on object when adding it to std::list