Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::map<Key,T,Compare,Allocator>::try_emplace

      From cppreference.com
      <cpp‎ |container‎ |map

      [edit template]
       
       
       
      std::map
      Member functions
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)
      Deduction guides(C++17)
       
      template<class...Args>
      std::pair<iterator,bool> try_emplace(const Key& k, Args&&...args);
      (1)(since C++17)
      template<class...Args>
      std::pair<iterator,bool> try_emplace( Key&& k, Args&&...args);
      (2)(since C++17)
      template<class K,class...Args>
      std::pair<iterator,bool> try_emplace( K&& k, Args&&...args);
      (3)(since C++26)
      template<class...Args>
      iterator try_emplace( const_iterator hint,const Key& k, Args&&...args);
      (4)(since C++17)
      template<class...Args>
      iterator try_emplace( const_iterator hint, Key&& k, Args&&...args);
      (5)(since C++17)
      template<class K,class...Args>
      iterator try_emplace( const_iterator hint, K&& k, Args&&...args);
      (6)(since C++26)

      If a key equivalent tok already exists in the container, does nothing. Otherwise, inserts a new element into the container with keyk and value constructed withargs. In such case:

      1) Behaves likeemplace except that the element is constructed as
      value_type(std::piecewise_construct,

                 std::forward_as_tuple(k),

                 std::forward_as_tuple(std::forward<Args>(args)...))
      2) Behaves likeemplace except that the element is constructed as
      value_type(std::piecewise_construct,

                 std::forward_as_tuple(std::move(k)),

                 std::forward_as_tuple(std::forward<Args>(args)...))
      3) Behaves likeemplace except that the element is constructed as
      value_type(std::piecewise_construct,

                 std::forward_as_tuple(std::forward<K>(k)),

                 std::forward_as_tuple(std::forward<Args>(args)...))
      4) Behaves likeemplace_hint except that the element is constructed as
      value_type(std::piecewise_construct,

                 std::forward_as_tuple(k),

                 std::forward_as_tuple(std::forward<Args>(args)...))
      5) Behaves likeemplace_hint except that the element is constructed as
      value_type(std::piecewise_construct,

                 std::forward_as_tuple(std::move(k)),

                 std::forward_as_tuple(std::forward<Args>(args)...))
      6) Behaves likeemplace_hint except that the element is constructed as
      value_type(std::piecewise_construct,

                 std::forward_as_tuple(std::forward<K>(k)),

                 std::forward_as_tuple(std::forward<Args>(args)...))
      1-6) Ifvalue_type is notEmplaceConstructible intomap from the corresponding expression, the behavior is undefined.
      3) This overload participates in overload resolution only if all following conditions are satisfied:
      Ifequal_range(u.first)== equal_range(k) isfalse, the behavior is undefined, whereu is the new element to be inserted.
      6) This overload participates in overload resolution only if the qualified-idCompare::is_transparent is valid and denotes a type.
      Ifequal_range(u.first)== equal_range(k) isfalse, the behavior is undefined, whereu is the new element to be inserted.

      No iterators or references are invalidated.

      Contents

      [edit]Parameters

      k - the key used both to look up and to insert if not found
      hint - iterator to the position before which the new element will be inserted
      args - arguments to forward to the constructor of the element

      [edit]Return value

      1-3) Same as foremplace:
      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.
      4-6) Same as foremplace_hint:
      An iterator to the inserted element, or to the element that prevented the insertion.

      [edit]Complexity

      1-3) Same as foremplace:
      Logarithmic in the size of the container.
      4-6) Same as foremplace_hint:
      Logarithmic in the size of the container in general, but amortized constant if the new element is inserted just beforehint.

      [edit]Notes

      Unlikeinsert oremplace, these functions do not move from rvalue arguments if the insertion does not happen, which makes it easy to manipulate maps whose values are move-only types, such asstd::map<std::string,std::unique_ptr<foo>>. In addition,try_emplace treats the key and the arguments to themapped_type separately, unlikeemplace, which requires the arguments to construct avalue_type (that is, astd::pair).

      Overloads(3) and(6) can be called without constructing an object of typeKey.

      Feature-test macroValueStdFeature
      __cpp_lib_map_try_emplace201411L(C++17)std::map::try_emplace,std::map::insert_or_assign
      __cpp_lib_associative_heterogeneous_insertion202311L(C++26)Heterogeneous overloads for the remaining member functions inordered andunordered associativecontainers. Overloads(3) and(6).

      [edit]Example

      Run this code
      #include <iostream>#include <string>#include <map>#include <utility> void print_node(constauto& node){std::cout<<'['<< node.first<<"] = "<< node.second<<'\n';} void print_result(autoconst& pair){std::cout<<(pair.second?"inserted: ":"ignored:  ");    print_node(*pair.first);} int main(){usingnamespace std::literals;std::map<std::string,std::string> m;     print_result(m.try_emplace("a","a"s));    print_result(m.try_emplace("b","abcd"));    print_result(m.try_emplace("c",10,'c'));    print_result(m.try_emplace("c","Won't be inserted")); for(constauto& p: m)        print_node(p);}

      Output:

      inserted: [a] = ainserted: [b] = abcdinserted: [c] = ccccccccccignored:  [c] = cccccccccc[a] = a[b] = abcd[c] = cccccccccc

      [edit]See also

      (C++11)
      constructs element in-place
      (public member function)[edit]
      constructs elements in-place using a hint
      (public member function)[edit]
      inserts elementsor nodes(since C++17)
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/map/try_emplace&oldid=135736"

      [8]ページ先頭

      ©2009-2025 Movatter.jp