Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rationale for C++'s std map insert semantics?

Tags:

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:

  • insert, replace if already exists
  • insert, ignore if already exists (this is the behavior of std::map)
  • insert, throw error if already exists
  • insert, UB if already exists

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.

like image 985
Martin Ba Avatar asked May 14 '12 14:05

Martin Ba


People also ask

What is the complexity of std::map :: insert () method?

Time complexity: k*log(n) where n is size of map, k is no. of elements inserted.

What does std::map insert return?

std::map::insert succeeds when it inserts the new element, otherwise it returns an iterator to an already existing element.

What is an insert on a map?

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.

How does map insert work C++?

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.


1 Answers

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.

like image 54
Kreg Avatar answered Sep 28 '22 16:09

Kreg