0

why insert to map does not change value

Please refer below code: https://code.sololearn.com/cA9A8a20a189/#cpp as we know , adding duplicate key does not change value into map element, but accessing it through key can modify it. Why so? I mean why duplicate insertion of key does not change value?

12th May 2021, 11:42 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Answers
+ 2
https://code.sololearn.com/cY66Zwmo8bxK/?ref=app If you look at this code it seems like that when you insert already existing key then it is not inserted at all But changing value at key access that key and change its value
12th May 2021, 11:57 AM
YUGRAJ
+ 2
Sometimes you need one or the other behavior so both exist. Semantically, since a map only contains unique keys, inserting a new key value pair should not work when there already is a value for that key in the map. On the other hand when directly accessing the value for a specific key in the map you should be able to modify it.
12th May 2021, 11:57 AM
Hape
Hape - avatar