|
set::insertpublic member function
pair<iterator,bool> insert ( const value_type& x ); iterator insert ( iterator position, const value_type& x ); template <class InputIterator> void insert ( InputIterator first, InputIterator last ); Insert element The set container is extended by inserting a single new element (if parameter x is used) or a sequence of elements (if input iterators are used).This effectively increases the container size by the amount of elements inserted. Because set containers do not allow for duplicate values, the insertion operation checks for each element inserted whether another element exists already in the container with the same value, if so, the element is not inserted and -if the function returns a value- an iterator to it is returned. For a similar container allowing for duplicate elements, see multiset. Internally, set containers keep all their elements sorted following the criterion specified by its comparison object on construction. Each element is inserted in its respective position following this ordering. Efficiency of this operation can be dramatically improved by providing the appropriate value as parameter position. Parameters
Return valueThe first version returns a pair, with its member pair::first set to an iterator pointing to either the newly inserted element or to the element that already had its same value in the set. The pair::second element in the pair is set to true if a new element was inserted or false if an element with the same value existed.The second version returns an iterator pointing to either the newly inserted element or to the element that already had its same value in the set. iterator is a member type, defined as a bidirectional iterator type. Example
Output:
ComplexityFor the first version ( insert(x) ), logarithmic.For the second version ( insert(position,x) ), logarithmic in general, but amortized constant if x is inserted right after the element pointed by position. For the third version ( insert (first,last) ), Nlog(size+N) in general (where N is the distance between first and last, and size the size of the container before the insertion), but linear if the elements between first and last are already sorted according to the same ordering criterion used by the container. See also
|