What is the difference between using the insert function when using std :: map.insert ("xyz") or just map [ind] = "xyz"

In the following example, what are the pros and cons of using 1) versus 2). Are there any advantages in memory allocation, any benefits if they are not exhausted?

map < int, string> Employees; // 1) Assignment using array index notation Employees[5234] = "Mike C."; // 2) Assignment using member function insert() and STL pair Employees.insert(std::pair<int, * char>(1923,"David D.")); 
+4
source share
5 answers

The first creates a mapping to key 5234 and returns a link to the string it contains, to which "Mike C" is assigned - an important point - if the key already exists, this will overwrite the value on this key (since the reference to the value is returned).

The second approach checks if the key is present and will not be overwritten if it already exists.

As for memory allocation, both sizes will increase the size of the card by 1 if the display does not exist. The above is the only difference between the two AFAIK approaches.

+5
source

The difference here is that option 1 will assign a value to the key in all cases where the second option can return std :: a pair containing the element iterator, plus a logical notification of the success of the insert. This means that if element 1923 already exists on your map, the insert function will return <iterator, false> , telling you that the key already exists on the map, and will not overwrite it.

 std::map<int, char*> aMap; aMap[12] = "An Entry"; std::pair< std::map<int, char*>::iterator, bool > result; result = aMap.insert(12, "A new entry"); //now result.first points to the existing entry at 12, result.second == false 

The only performance benefit I can think of is that you save processing time by not executing if(aMap.count(12) == 0) or if(aMap.find(12) == aMap.end()) before performing an insert operation.

+4
source
  • If key does not exist on the map, then Employees[key] = value adds a new key-value pair to the map.

  • If key already exists, then Employees[key] = value simply updates the value with the given key.

  • Employees.insert() always adds a new key-value pair to the map. However, if the key already exists, it does nothing. If you want different elements to have the same key, you can use multimap .

+4
source

For more information, see Scott Meyers’s Effective STL (Topic 24).

+1
source

It is very useful to know this difference between insert and operator[]

In fact, you can implement operator[] in terms of insert , since it returns pair<iterator,bool>

Thus, it is possible (and possibly) to implement:

 Value& map<K,T>::operator[]( const Key& key ) { return insert( make_pair(key, Value()) ).first->second; } 

Practical application: In a typical situation with "lazy loading" you may have map<Key, shared_ptr<Value> >

Typically, a programmer begins with a search:

  const_iterator iter = theMap.find(key); if( iter!= theMap.end()) return iter->second; else { // create the item then insert it } 

The best approach:

 shared_ptr<Value> value = theMap[key]; if( !value ) { value.reset( /*load logic */ ) } return value; 

In the multi-threaded situation above, instead of shared_ptr, use a wrapper class that implements the logic "boost :: once". Let's say that loading an object takes a lot of time. In the classic case, you are likely to block the entire map while you search for the item, and then create it. Thus, no other thread can use the card at all.

Instead, we now block the map for search only and create an object in the "ONCE_INIT" state. Then we unlock the card and use boost::call_once to initialize the actual item. Please note that during this time it is possible that another thread created an element different from the one that added it to the map, but this does not matter: it will be created exactly once, and there is no thread conflict. (This is a write-level lock implementation). Note that if we used the first construct (find then insert), we would not be able to do this.

+1
source

Source: https://habr.com/ru/post/1338866/


All Articles