Scorb
Scorb

Reputation: 1950

Why does std::map::insert docs refer to a key argument that does not exist

I am looking at the docs for std::map::insert.

The function signature with a "hint" is defined as follows....

with hint (2)   
iterator insert (const_iterator position, const value_type& val);
template <class P> iterator insert (const_iterator position, P&& val);

Then the subsequent description of the return value for that particular implementation of insert is as follows...

The versions with a hint (2) return an iterator pointing to either the newly inserted element or to the element that already had an equivalent key in the map.

But this doesn't make any sense, as I never provided a key as an argument to this function, only a value.

So what exactly will it return?

Upvotes: 0

Views: 64

Answers (1)

Pavan Chandaka
Pavan Chandaka

Reputation: 12821

Your key is a part of value you pass to the value_type.

You pass value something like below, in which "1" is key and "100" is value.

std::pair<int,int>(1,100)

or

 std::make_pair(1, 100)

example:

std::map<int, int> testmap;

testmap.insert(testmap.begin(),std::make_pair(1, 100));
testmap.insert(testmap.begin(),std::pair<int,int>(2, 100));

Upvotes: 2

Related Questions