Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why set/map emplace_hint does not return a boolean

Tags:

c++

c++11

stl

According to cppreference, both std::set and std::map emplace functions return a std::pair<iterator,bool>, with a bool value to say if the insertion actually took place.

However, emplace_hint returns an iterator either to the inserted element or to the existing element in the set or map if the insertion didn't happen. There is no bool value here.

Is there any reason for this difference in the interface of these similar functions?

Update

Function insert returns the bool value only when no hint is provided. This is consistent with the behavior of emplace and emplace_hint. The question is then: is there any reason to not return a bool when a hint is given?

I can only think that maybe there is some performance reason, because the user usually provides a hint after a lower_bound/upper_bound operation, so it's sure the insertion will happen.

like image 501
ChronoTrigger Avatar asked Oct 06 '15 12:10

ChronoTrigger


People also ask

What does insert return in map?

Return Value: The function returns a pair, with its member pair::first set to an iterator pointing to either the newly inserted element or to the element with an equivalent key in the map.

How to insert value to map?

The standard solution to insert new elements into a map is using the std::map::insert function. It inserts the specified key-value pair into the map only if the key already doesn't exist. If the key already exists in the map, the element is not inserted.

How to insert pair in map c++?

int a=10,b=20; map < pair < int,int >, int > m; pair < int,int >numbers = make_pair(a,b); int sum=a+b; m[numbers]=sum; Our map will have its key as pairs of numbers. We can access the integer values of pair variable using dot(.) operator.

Does map insert overwrite?

insert() doesn't overwrite.


2 Answers

emplace_hint does that likely for consistency with the hinted insert: emplace was initially proposed as a pair of overloads, mirroring insert, but the hinted overload was renamed after LWG 763, although Josuttis wanted to rename the non-hinted version instead)

The hinted insert for associative containers takes an iterator and a value and returns an iterator in order to be compatible with the regular insert on sequential containers in generic code. as mentioned in Josuttis's book. This compatibility is exploited by std::inserter

like image 88
Cubbi Avatar answered Oct 05 '22 14:10

Cubbi


The easiest way to determine, whether the emplace took place or not, is to store the size() of the map in a variable and test, whether that size has increased after the emplace_hint():

auto oldsz = myMap.size();
myMap.emplace_hint(it, args...);
if(myMap.size() > oldsz) {
    // emplace was accepted
} else {
    // the emplace was rejected, as it would have overwritten an element
}

The same code can also be used with insert_or_assign(), if the new value shall overwrite the old in case of an already existing key.

like image 28
Kai Petzke Avatar answered Oct 05 '22 14:10

Kai Petzke