13

What is the difference between the index overloaded operator and the insert method call for std::map?

ie:

some_map["x"] = 500;

vs.

some_map.insert(pair<std::string, int>("x", 500));
TheOne
  • 10,819
  • 20
  • 81
  • 119
  • The first one is much more readable ;) – schnaader Oct 20 '09 at 13:36
  • 3
    Using make_pair() would help on the insert because it can deduce the argument types: "some_map.insert(std::make_pair("x", 500));" – Fred Larson Oct 20 '09 at 13:47
  • @Fred Larson: Although the `make_pair` you show would deduce `const char*` and `int` so an implicit conversion from one `pair` type to the actual `value_type` of the `map` will be generated. – CB Bailey Oct 20 '09 at 14:12
  • possible duplicate of [In STL maps, is it better to use map::insert than \[\]?](http://stackoverflow.com/questions/326062/in-stl-maps-is-it-better-to-use-mapinsert-than) – user Mar 11 '14 at 17:08

5 Answers5

22

I believe insert() will not overwrite an existing value, and the result of the operation can be checked by testing the bool value in the iterator/pair value returned

The assignment to the subscript operator [] just overwrites whatever's there (inserting an entry if there isn't one there already)

Either of the insert and [] operators can cause issues if you're not expecting that behaviour and don't accommodate for it.

Eg with insert:

std::map< int, std::string* > intMap;
std::string* s1 = new std::string;
std::string* s2 = new std::string;
intMap.insert( std::make_pair( 100, s1 ) ); // inserted
intMap.insert( std::make_pair( 100, s2 ) ); // fails, s2 not in map, could leak if not tidied up

and with [] operator:

std::map< int, std::string* > intMap;
std::string* s1 = new std::string;
std::string* s2 = new std::string;
intMap[ 100 ] = s1; // inserted
intMap[ 100 ] = s2; // inserted, s1 now dropped from map, could leak if not tidied up

I think those are correct, but haven't compiled them, so may have syntax errors

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
pxb
  • 747
  • 4
  • 14
  • You were right, map[i] is good for overwrite, but map.insert() does not overwrite sometimes, I recently faced this behaviour. – Saurabh Singh Jun 10 '18 at 16:45
8

For a map, the former (operator[]) expression will always replace the value part of the key-value pair with the new supplied value. A new key-value pair will be inserted if one doesn't already exist.

In contrast, insert will only insert a new key-value pair if a key-value pair with the supplied key part does not already exist in the map.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
5

In addition to the fact that map::operator[] will replace an existing value is that operator[] map::will create and add to the map a default existing value to replace before the replacement occurs (the map::operator[]() call has to return a reference to something). For items that are expensive to create this could be a performance issue.

See "Item 24: Choose carefully between map::operator[] and map::insert when efficiency is important" in Scott Meyers' Effective STL.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
0

The insert method inserts into the map, while the overloaded index operator will return the element with the key key_value if it is in the map, if it is not already in the map then it will insert it.

Brandon Haugen
  • 961
  • 6
  • 25
  • It'll only insert into the map if a given key is not already present. If it is present, insert() will not update the value whereas an assignment to operator[] will. – Timo Geusch Oct 20 '09 at 13:44
0

Just to add to Michael Burr's answer, the book you should be looking for is Scott Meyer's <Effective STL> not the <Effective C++>, as Michael has linked it wrongly.

the expression

m[k] = v;

  checks to see if the key k is already in the map. If not, it’s added, along with v as its corresponding value. If k is already in the map, its associated value is updated to v.

(when adding new k-v)

We first default-construct a <value obj>, then we immediately assign it a new value. If it’s measurably more efficient to construct a <value obj> with the value we want instead of default-constructing the <value obj> and then doing the assignment, we’d be better off replacing our use of operator[] (including its attendant construction plus assignment) with a straightforward call to insert:

(updating existing k's v)

we now understand that when an “add” is performed, insert is more efficient than operator[]. The situation is reversed when we do an update, i.e., when an equivalent key is already in the map.

Minsoo
  • 41
  • 3