Recommended way to insert items into a map

Possible duplicate:
Is it better to use map :: insert in STL maps than []?

I was wondering when I insert an element into the map, what is the recommended way. Should I

map[key] = value; 

or

 map.insert(std::pair<key_type, value_type>(key, value)); 

I did the following quick test:

 #include <map> #include <string> #include <iostream> class Food { public: Food(const std::string& name) : name(name) { std::cout << "constructor with string parameter" << std::endl; } Food(const Food& f) : name(f.name) { std::cout << "copy" << std::endl; } Food& operator=(const Food& f) { name = f.name; std::cout << "=" << std::endl; return *this; } Food() { std::cout << "default" << std::endl; } std::string name; }; int main() { std::map<std::string, Food> m0; /* 1) constructor with string parameter 2) copy 3) copy 4) copy */ m0.insert(std::pair<std::string, Food>("Key", Food("Ice Cream"))); /* 1) constructor with string parameter 2) default 3) copy 4) copy 5) = */ // If we do not provide default constructor. // C2512: 'Food::Food' : no appropriate default constructor available m0["Key"] = Food("Ice Cream"); } 
  • I understand, using the insert member function, a smaller function call will be involved. So insert recommended method?
  • Why is a default constructor needed when the map[key] = value method is used?

I know that insert does not overwrite a pair of existence values, but map[key] = value does. However, is this the only factor that I take into account when I try to choose from both?

What about

  • Performance
  • Default Constructor Availability
  • ???
+56
c ++ stl stdmap
Aug 05 2018-11-11T00:
source share
4 answers
  • insert not recommended - this is one way to insert into a card. The difference with operator[] is that insert can determine if an element is inserted in the map. Also, if your class does not have a default constructor, you are forced to use insert .
  • operator[] needs a default constructor because the map checks to see if an element exists. If this is not the case, it creates one using the default constructor and returns a link (or a constant link to it).

Since map containers do not allow duplicate key values, the insert operation checks whether each element is inserted, whether there is another element in the container with the same key value, if so, the element is not inserted, and its displayed value is not changed in any way .

+37
Aug 05 2018-11-11T00:
source share

Use insert if you want to insert a new element. insert will not overwrite an existing element, and you can make sure that there was no previously exiting element:

 if ( !myMap.insert( std::make_pair( key, value ) ).second ) { // Element already present... } 

Use [] if you want to overwrite a possibly existing item:

 myMap[ key ] = value; assert( myMap.find( key )->second == value ); // post-condition 

This form will overwrite any existing record.

+29
Aug 05 2018-11-11T00:
source share

Quote:

Since map containers do not allow duplicate key values, the insert operation checks whether each element is inserted, whether there is another element in the container with the same key value, if so, the element is not inserted, and its displayed value is not changed in any way .

Thus, the insertion will not change the value, if the key already exists, the [] operator will be.

EDIT:

This reminds me of another recent question: why use at () instead of the [] operator to extract values ​​from a vector. Apparently, an exception is thrown in () if the index goes beyond, while [] does not. In these situations, it is always better to look for documentation of functions, as they will give you all the details. But in the general case there are no (or at least should not be) two functions / operators that do the same thing.

I assume that inside, insert () will first check for writing, and then will use the [] operator itself.

+11
Aug 05 2018-11-11T00:
source share

map[key] = value provided to simplify the syntax. Easier to read and write.

The reason you need the default constructor is because map[key] is evaluated before assignment. If there is no key on the map, a new one is created (with a standard constructor), and a link to it is returned from operator[] .

+8
Aug 05 2018-11-11T00:
source share



All Articles