Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::set<Key,Compare,Allocator>::insert

      From cppreference.com
      <cpp‎ |container‎ |set
       
       
       
       
      std::pair<iterator,bool> insert(const value_type& value);
      (1)(constexpr since C++26)
      std::pair<iterator,bool> insert( value_type&& value);
      (2)(since C++11)
      (constexpr since C++26)
      (3)
      iterator insert( iterator pos,const value_type& value);
      (until C++11)
      iterator insert( const_iterator pos,const value_type& value);
      (since C++11)
      (constexpr since C++26)
      iterator insert( const_iterator pos, value_type&& value);
      (4)(since C++11)
      (constexpr since C++26)
      template<class InputIt>
      void insert( InputIt first, InputIt last);
      (5)(constexpr since C++26)
      void insert(std::initializer_list<value_type> ilist);
      (6)(since C++11)
      (constexpr since C++26)
      insert_return_type insert( node_type&& nh);
      (7)(since C++17)
      (constexpr since C++26)
      iterator insert( const_iterator pos, node_type&& nh);
      (8)(since C++17)
      (constexpr since C++26)
      template<class K>
      std::pair<iterator,bool> insert( K&& x);
      (9)(since C++23)
      (constexpr since C++26)
      template<class K>
      iterator insert( const_iterator pos, K&& x);
      (10)(since C++23)
      (constexpr since C++26)

      Attempts to insert element(s) into*this.

      • If*this already contain an element with an equivalent key, does nothing.
      • Otherwise, inserts the element(s) into*this.
      1-4) Insertsvalue. Ifpos is provided,value will be inserted as close as possible to the position just prior topos.
      1,3) Ifvalue_type is notCopyInsertable intoset, the behavior is undefined.
      2,4) Ifvalue_type is notMoveInsertable intoset, the behavior is undefined.
      (since C++11)
      5) Inserts elements from range[firstlast).
      If any of the following conditions is satisfied, the behavior is undefined:
      (since C++11)
      • first orlast is an iterator into*this.
      6) Inserts elements from initializer listilist.
      Equivalent toinsert(ilist.begin(), ilist.end()).
      7) Ifnh is an emptynode handle, does nothing. Otherwise, inserts the element owned bynh into the container , if the container doesn't already contain an element with a key equivalent tonh.key(). The behavior is undefined ifnh is not empty andget_allocator()!= nh.get_allocator().
      8) Ifnh is an emptynode handle, does nothing and returns the end iterator. Otherwise, inserts the element owned bynh into the container, if the container doesn't already contain an element with a key equivalent tonh.key(), and returns the iterator pointing to the element with key equivalent tonh.key()(regardless of whether the insert succeeded or failed). If the insertion succeeds,nh is moved from, otherwise it retains ownership of the element. The element is inserted as close as possible to the position just prior topos. The behavior is undefined ifnh is not empty andget_allocator()!= nh.get_allocator().
      9,10) Constructs an objectu of typevalue_type withstd::forward<K>(x) and then insertsu into*this. The existence of the equivalent key is determined transparently usingx before constructingu.
      If any of the folllowing conditions is satisfied, the behavior is undefined:
      9) This overload participates in overload resolution only ifCompare istransparent.
      10)u will be inserted as close as possible to the position just prior topos.
      This overload participates in overload resolution only if all following conditions are satisfied:

      No iterators or references are invalidated.If the insertion is successful, pointers and references to the element obtained while it is held in the node handle are invalidated, and pointers and references obtained to that element before it was extracted become valid.(since C++17)

      Contents

      [edit]Parameters

      pos - iterator to the position before which the new element will be inserted
      value - element value to insert
      first, last - the pair of iterators defining the sourcerange of elements to insert
      ilist - initializer list to insert the values from
      nh - a compatiblenode handle
      x - a value of any type that can be transparently compared with a key
      Type requirements
      -
      InputIt must meet the requirements ofLegacyInputIterator.

      [edit]Return value

      1,2) A pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and abool value set totrue if and only if the insertion took place.
      3,4) An iterator to the inserted element, or to the element that prevented the insertion.
      7) An object ofinsert_return_type with the members initialized as follows:
      • Ifnh is empty,inserted isfalse,position isend(), andnode is empty.
      • Otherwise if the insertion took place,inserted istrue,position points to the inserted element, andnode is empty.
      • If the insertion failed,inserted isfalse,node has the previous value ofnh, andposition points to an element with a key equivalent tonh.key().
      8) End iterator ifnh was empty, iterator pointing to the inserted element if insertion took place, and iterator pointing to an element with a key equivalent tonh.key() if it failed.
      9) A pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and abool value set totrue if and only if the insertion took place.
      10) An iterator to the inserted element, or to the element that prevented the insertion.

      [edit]Exceptions

      If an exception is thrown by any operation during the insertion of a single element, the insertion has no effect.

      [edit]Complexity

      Given\(\scriptsize N\)N assize():

      1,2)\(\scriptsize \log(N)\)log(N)
      3,4) Amortized constant if the insertion happens in the position justafter(until C++11)before(since C++11)pos,\(\scriptsize \log(N)\)log(N) otherwise.
      5,6)\(\scriptsize \log(N + M)\)log(N+M), where\(\scriptsize M\)M is the number of elements to insert.
      7)\(\scriptsize \log(N)\)log(N)
      8) Amortized constant if the insertion happens in the position just beforepos,\(\scriptsize \log(N)\)log(N) otherwise.
      9)\(\scriptsize \log(N)\)log(N)
      10) Amortized constant if the insertion happens in the position just beforepos,\(\scriptsize \log(N)\)log(N) otherwise.

      [edit]Notes

      The hinted insert((3,4),(8) and(10))does not return a boolean in order to be signature-compatible with positional insert on sequential containers, such asstd::vector::insert. This makes it possible to create generic inserters such asstd::inserter. One way to check success of a hinted insert is to comparesize() before and after.

      The overloads(5,6) are often implemented as a loop that calls the overload(3) withend() as the hint; they are optimized for appending a sorted sequence (such as anotherstd::set) whose smallest element is greater than the last element in*this.

      If multiple elements in the range have keys that compare equivalent, it is unspecified which element is inserted (pendingLWG2844).

      Feature-test macroValueStdFeature
      __cpp_lib_associative_heterogeneous_insertion202311L(C++26)Heterogeneous overloads for the remaining member functions inordered andunordered associative containers.(9,10)

      [edit]Example

      Run this code
      #include <cassert>#include <iostream>#include <set> int main(){std::set<int> set; auto result_1= set.insert(3);assert(result_1.first!= set.end());// it is a valid iteratorassert(*result_1.first==3);if(result_1.second)std::cout<<"insert done\n"; auto result_2= set.insert(3);assert(result_2.first== result_1.first);// same iteratorassert(*result_2.first==3);if(!result_2.second)std::cout<<"no insertion\n";}

      Output:

      insert doneno insertion

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 233C++98pos was just a hint, it could be totally ignoredthe insertion is required to
      be as close as possible to the
      position just prior topos
      LWG 264C++98the complexity of overload(5) was required to be linear if
      the range[firstlast) is sorted according toCompare
      removed the linear requirement
      in this special case
      LWG 316C++98in the return value of overload(1), it was not specified
      whichbool value indicates a successful insertion
      success is indicated bytrue

      [edit]See also

      (C++11)
      constructs element in-place
      (public member function)[edit]
      constructs elements in-place using a hint
      (public member function)[edit]
      creates astd::insert_iterator of type inferred from the argument
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/set/insert&oldid=183330"

      [8]ページ先頭

      ©2009-2025 Movatter.jp