STL里map就是这样实现的。没有为什么。
m.insert(e) e is a value of the value_type from m. If the key (e.first)is not in m,insert a new element with value e.second. If the key is in m,then m is unchanged.
Returns a pair containing a map iterator referring to the element with key e.first and a bool indicating whether the element was inserted or not.
The first insert member function returns a pair whose bool component returns true if an insertion was made and false if the map already contained an element whose key had an equivalent value in the ordering, and whose iterator component returns the address where a new element was inserted or where the element was already located.
To access the iterator component of a pair pr returned by this member function, use pr.first, and to dereference it, use *(pr.first). To access the bool component of a pair pr returned by this member function, use pr.second.
The second insert member function, the hint version, returns an iterator that points to the position where the new element was inserted into the map.
insert
Syntax:
#include <map>
iterator insert( iterator i, const TYPE& pair );
void insert( input_iterator start, input_iterator end );
pair<iterator,bool> insert( const TYPE& pair );
The function insert() either:
● inserts pair after the element at pos (where pos is really just a suggestion as to where pair
should go, since sets and maps are ordered), and returns an iterator to that element.
● inserts a range of elements from start to end.
--------------
● inserts pair, but only if pair doesn't already exist. The return value is an iterator to the element
inserted, and a boolean describing whether an insertion took place.