(C++17) | ||||
| Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
| Associative | ||||
| Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
| Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Views | ||||
(C++20) | ||||
(C++23) | ||||
| Tables | ||||
| Iterator invalidation | ||||
| Member function table | ||||
| Non-member function table |
| Member types | ||||||
| Member functions | ||||||
| Non-member functions | ||||||
| ||||||
| 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:
emplace except that the element is constructed asemplace except that the element is constructed as std::forward_as_tuple(std::move(k)),
emplace except that the element is constructed as std::forward_as_tuple(std::forward<K>(k)),
emplace_hint except that the element is constructed asemplace_hint except that the element is constructed as std::forward_as_tuple(std::move(k)),
emplace_hint except that the element is constructed as std::forward_as_tuple(std::forward<K>(k)),
value_type is notEmplaceConstructible intounordered_map from the corresponding expression, the behavior is undefined.If after the operation the new number of elements is greater than oldmax_load_factor() * bucket_count() a rehashing takes place.
If rehashing occurs (due to the insertion), all iterators are invalidated. Otherwise (no rehashing), iterators are not invalidated.
Contents |
| 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 |
emplace:emplace_hint:emplace:emplace_hint: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::unordered_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 macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_unordered_map_try_emplace | 201411L | (C++17) | std::unordered_map::try_emplace,std::unordered_map::insert_or_assign |
__cpp_lib_associative_heterogeneous_insertion | 202311L | (C++26) | Heterogeneous overloads for the remaining member functions inordered andunordered associativecontainers. Overloads(3) and(6). |
#include <iostream>#include <string>#include <unordered_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::unordered_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);}
Possible output:
inserted: [a] = ainserted: [b] = abcdinserted: [c] = ccccccccccignored: [c] = cccccccccc[a] = a[b] = abcd[c] = cccccccccc
| 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] |