I'm a little bit confused by std::map::insert
's semantics. I mean, I'm not complaining - the standard is the standard and the API is the way it is. Still,
insert
will
the insertion operation checks for each element inserted whether another element exists already in the container with the same key value, if so, the element is not inserted and its mapped value is not changed in any way.
And - only in its single-argument version pair<iterator,bool> insert ( const value_type& x );
will it even tell you if it even inserted the (new, possibly different) value to the key(s). As far as I understand, the iterator versions will silently ignore insertions if the key already exists.
For me, this is simply counter intuitive, I would have expected the value part to be overwritten and the old value part to be discarded on insert. Obviously, the designers of the STL thought differently -- anyone knows the (historical) rationale or can give a thorough explanation of how the existing semantics make (more) sense?
By example:
There are a few basic ways to implement insert in a single-key map such as std::map
:
I'm now trying to understand why insert_or_ignore
makes more sense than insert_or_replace
(or insert_or_error
)!
I looked into my copy of TC++PL (unfortunately I only have the German edition), and interestingly, Stroustrup writes in chapter 17.4.1.7 (list operations for map): (sorry rough translation from German)
(...) Normally, one doesn't care whether a key(sic!) is newly inserted or already existed before the call to
insert()
(...)
Which, it seems to me, would only hold true for set, and not for map, because for a map, it does make quite some difference if the provided value was inserted or the old one remains in the map. (It obviously doesn't matter for the key, as that one is equivalent.)
Note: I know about operator[]
and I know about Item 24 of Effective STL and the there proposed efficientAddOrUpdate
function. I'm just curious for a rationale into insert
's semantics because I personally find them counter intuitive.
Time complexity: k*log(n) where n is size of map, k is no. of elements inserted.
std::map::insert succeeds when it inserts the new element, otherwise it returns an iterator to an already existing element.
An inset map is a smaller map featured on the same page as the main map. Traditionally, inset maps are shown at a larger scale (smaller area) than the main map. Often, an inset map is used as a locator map that shows the area of the main map in a broader, more familiar geographical frame of reference.
map::insert() function is an inbuilt function in C++ STL, which is defined in header file. insert() is used to insert new values to the map container and increases the size of container by the number of elements inserted.
The insert method is just not what you are looking for, it sounds like... The insert method is made to do just what the name implies... insert values. I agree that the ability to create a value if one isn't already present, and replace the one that is there otherwise is important in some situations, but in others you would just really rather not handle exceptions, return values, etc if you just want to do an insert only if the value isn't already present.
It sounds like the method you're looking for (as BoBTFish indicated above) is probably the []
operator. Just use it like so:
myMap["key"] = "value";
This will go through your map and find the key "key", and replace the corresponding value with "value". If the key isn't there, it will create it. Both methods are very useful in different situations, and I've found myself using both just depending on what I need.
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