In the following example what is the pros and cons of using 1) versus 2). Is there any memory allocation benefits, any benefits as far as not running out of space?
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."));
The first creates a mapping with key 5234 and returns a reference to the string held there, to which "Mike C" gets assigned - important point is, if the key already exists, this will overwrite the value at that key (because a reference to the value is returned).
The second approach checks to see if the key is present, and will not overwrite if it does already exist.
As for memory allocation, both will increase the map size by 1 if the mapping does not exist. The above is the only difference between the two approaches AFAIK.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With