A while ago, I had a discussion with a colleague about how to insert values in STL maps. I preferred map[key] = value; because it feels natural and is clear to read whereas he preferred map.insert(std::make_pair(key, value)).
I just asked him and neither of us can remember the reason why insert is better, but I am sure it was not just a style preference rather there was a technical reason such as efficiency. The SGI STL reference simply says: "Strictly speaking, this member function is unnecessary: it exists only for convenience."
Can anybody tell me that reason, or am I just dreaming that there is one?
When you write
there’s no way to tell if you replaced the
valueforkey, or if you created a newkeywithvalue.map::insert()will only create:For most of my apps, I usually don’t care if I’m creating or replacing, so I use the easier to read
map[key] = value.