About the STL container set and multiset,the return types of the insert functions are not all the same.
set provides the following interface:
pair<iterator,bool> insert(const value_type& elem);
iterator insert(iterator pos_hint, const value_type& elem);
multiset provides the following interface:
iterator insert(const value_type& elem);
iterator insert(iterator pos_hint, const value_type& elem);
In the first function of set, the member second of the pair structure returns whether the insertion is successful.The insertion of an element might fail for a set if it already contains an element with the same value.But in the second function of set,the insert function just returns an iterator.What will happen if the insertion fails?Could someone tell me that?
Thanks a lot.
In the
set::insertversion that just returns a plainiterator(and not apair<iterator,bool>), when an existing element is found the set is left unchanged and theinsertwill return an iterator to the existing element (that prevented the insertion).In
multiset::insert, the function is always successful.23.2.4 Associative Container Requirements
iterator a.insert(p, t)