Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |container‎ |flat map
       
       
       
      std::flat_map
      Member types
      Member functions
      Non-member functions
      Helper classes
      Tags
      Deduction guides
       
      template<class...Args>
      std::pair<iterator,bool> try_emplace(const key_type& k, Args&&...args);
      (1)(since C++23)
      template<class...Args>
      std::pair<iterator,bool> try_emplace( key_type&& k, Args&&...args);
      (2)(since C++23)
      template<class K,class...Args>
      std::pair<iterator,bool> try_emplace( K&& k, Args&&...args);
      (3)(since C++23)
      template<class...Args>
      iterator try_emplace( const_iterator hint,const key_type& k, Args&&...args);
      (4)(since C++23)
      template<class...Args>
      iterator try_emplace( const_iterator hint, key_type&& k, Args&&...args);
      (5)(since C++23)
      template<class K,class...Args>
      iterator try_emplace( const_iterator hint, K&& k, Args&&...args);
      (6)(since C++23)

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

      1,2,4,5) Equivalent to:
      auto key_it=ranges::upper_bound(c.keys, k, compare);auto value_it= c.values.begin()+std::distance(c.keys.begin(), key_it);c.keys.insert(key_it,std::forward<decltype(k)>(k));c.values.emplace(value_it,std::forward<Args>(args)...);
      3,6) Equivalent to:
      auto key_it=ranges::upper_bound(c.keys, k, compare);auto value_it= c.values.begin()+std::distance(c.keys.begin(), key_it);c.keys.emplace(key_it,std::forward<K>(k));c.values.emplace(value_it,std::forward<Args>(args)...);
      The conversion fromk intokey_type must construct an objectu, for whichfind(k)== find(u) istrue. Otherwise, the behavior is undefined.
      These overloads participate in overload resolution only if :
      Information on iterator invalidation is copied fromhere

      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.
      4-6) Same as foremplace_hint.

      [edit]Complexity

      1-3) Same as foremplace.
      4-6) Same as foremplace_hint.

      [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::flat_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,6) can be called without constructing an object of typekey_type.

      [edit]Example

      Run this code
      #include <flat_map>#include <iostream>#include <string>#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

      constructs element in-place
      (public member function)[edit]
      constructs elements in-place using a hint
      (public member function)[edit]
      inserts elements
      (public member function)[edit]
      inserts an element or assigns to the current element if the key already exists
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/flat_map/try_emplace&oldid=171975"

      [8]ページ先頭

      ©2009-2025 Movatter.jp