Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::insert

      From cppreference.com
      <cpp‎ |container‎ |flat map
       
       
       
      std::flat_map
      Member types
      Member functions
      Non-member functions
      Helper classes
      Tags
      Deduction guides
       
      std::pair<iterator,bool> insert(const value_type& value);
      (1)(since C++23)
      std::pair<iterator,bool> insert( value_type&& value);
      (2)(since C++23)
      iterator insert( const_iterator pos,const value_type& value);
      (3)(since C++23)
      iterator insert( const_iterator pos, value_type&& value);
      (4)(since C++23)
      template<class P>
      std::pair<iterator,bool> insert( P&& x);
      (5)(since C++23)
      template<class P>
      iterator insert( const_iterator pos, P&& x);
      (6)(since C++23)
      template<class InputIt>
      void insert( InputIt first, InputIt last);
      (7)(since C++23)
      template<class InputIt>
      void insert(std::sorted_unique_t, InputIt first, InputIt last);
      (8)(since C++23)
      void insert(std::initializer_list<key_type> ilist);
      (9)(since C++23)
      void insert(std::sorted_unique_t s,std::initializer_list<key_type> ilist);
      (10)(since C++23)

      Inserts element(s) into the container, if the container does not already contain an element with an equivalent key.

      1) Insertsvalue. Equivalent toreturn emplace(value);.
      2) Insertsvalue. Equivalent toreturn emplace(std::move(value));.
      3) Insertsvalue in the position as close as possible to the position just prior topos. Equivalent toreturn emplace_hint(pos, value);.
      4) Insertsvalue in the position as close as possible to the position just prior topos. Equivalent toreturn emplace_hint(pos, std::move(value));.
      5) If*this already contains an element which transparently comparesequivalent tox, does nothing. Otherwise, insertsx into*this as if byemplace(std::forward<P>(x));. This overload participates in overload resolution only ifstd::is_constructible_v<std::pair<key_type, mapped_type>, P> istrue.
      6) If*this already contains an element which transparently comparesequivalent tox, does nothing. Otherwise, insertsx into*this in the position as close as possible to the position just prior topos. Equivalent toreturn emplace_hint(pos,std::forward<P>(x));. This overload participates in overload resolution only ifstd::is_constructible_v<std::pair<key_type, mapped_type>, P> istrue.
      7) Inserts elements from range[firstlast) as if performing the following operations sequentially:
      1. Adds elements toc as if by
        for(; first!= last;++first)
        {
            value_type value=*first;
            c.keys.insert(c.keys.end(), std::move(value.first));
            c.values.insert(c.values.end(), std::move(value.second));
        }
      2. Sorts the range of newly inserted elements with respect tovalue_comp.
      3. Merges the resulting sorted range and the sorted range of pre-existing elements into a single sorted range.
      4. Erases the duplicate elements as if by:
        auto zv= std::views::zip(c.keys, c.values);
        auto it=ranges::unique(zv, key_equiv(compare)).begin();
        auto dist=std::distance(zv.begin(), it);
        c.keys.erase(c.keys.begin()+ dist, c.keys.end());
        c.values.erase(c.values.begin()+ dist, c.values.end());
      May allocate memory during the in-place merge operation.
      If multiple elements in the range have keys that compare equivalent, it is unspecified which element is inserted (pendingLWG2844).
      8) Inserts elements from range[firstlast) as if performing the following operations sequentially:
      1. Adds elements toc as if by
        for(; first!= last;++first)
        {
            value_type value=*first;
            c.keys.insert(c.keys.end(), std::move(value.first));
            c.values.insert(c.values.end(), std::move(value.second));
        }
      2. Merges the sorted range of newly added elements and the sorted range of pre-existing elements into a single sorted range.
      3. Erases the duplicate elements as if by:
        auto zv= std::views::zip(c.keys, c.values);
        auto it=ranges::unique(zv, key_equiv(compare)).begin();
        auto dist=std::distance(zv.begin(), it);
        c.keys.erase(c.keys.begin()+ dist, c.keys.end());
        c.values.erase(c.values.begin()+ dist, c.values.end());
      May allocate memory during the in-place merge operation.
      If multiple elements in the range have keys that compare equivalent, it is unspecified which element is inserted (pendingLWG2844).
      9) Inserts elements from initializer listilist. Equivalent toinsert(ilist.begin(), ilist.end());.
      If multiple elements in the range have keys that compare equivalent, it is unspecified which element is inserted (pendingLWG2844).
      10) Inserts elements from initializer listilist. Equivalent toinsert(s, ilist.begin(), ilist.end());.
      If multiple elements in the range have keys that compare equivalent, it is unspecified which element is inserted (pendingLWG2844).
      Information on iterator invalidation is copied fromhere

      Contents

      [edit]Parameters

      pos - an iterator to the position before which the new element will be inserted
      value - an element value to insert
      first, last - the pair of iterators defining the sourcerange of elements to insert
      ilist - an initializer list to insert the values from
      x - a value of any type that can be transparently compared with a key
      s - a disambiguation tag indicating that the input sequence is sorted (with respect tovalue_comp()) and contains only unique elements
      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.
      5) 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.
      6) An iterator to the inserted element, or to the element that prevented the insertion.
      7-10) (none)

      [edit]Exceptions

      1-6) If an exception is thrown by any operation, the insertion has no effect.
      This section is incomplete
      Reason: cases 7-10

      [edit]Complexity

      1-6) Linear insize().
      7)N+ M·log(M), whereN is thesize() before the operation andM isstd::distance(first, last).
      8) Linear insize().
      9)N+ M·log(M), whereN is thesize() before the operation andM isilist.size().
      10) Linear inN, whereN issize() after the operation.

      [edit]Notes

      The hinted insert((3,4) and(6))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.

      [edit]Example

      This section is incomplete
      Reason: no example

      [edit]See also

      constructs element in-place
      (public member function)[edit]
      constructs elements in-place using a hint
      (public member function)[edit]
      inserts an element or assigns to the current element if the key already exists
      (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/flat_map/insert&oldid=183132"

      [8]ページ先頭

      ©2009-2025 Movatter.jp