std::unordered_map::try_emplace - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::unordered_map::try_emplace

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

[edit template]

template <class... Args> pair<iterator, bool> try_emplace(const key_type& k, Args&&... args); | (1) | (since C++17)
template <class... Args> pair<iterator, bool> try_emplace(key_type&& k, Args&&... args); | (2) | (since C++17)
template <class... Args> iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args); | (3) | (since C++17)
template <class... Args> iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args); | (4) | (since C++17)

1) If a key equivalent to `k` already exists in the container, does nothing. Otherwise, behaves like emplace except that the element is constructed as `value_type(std::piecewise_construct, std::forward_as_tuple(k), std::forward_as_tuple(forward<Args>(args)...))`
2) If a key equivalent to `k` already exists in the container, does nothing. Otherwise, behaves like emplace except that the element is constructed as `value_type(std::piecewise_construct, std::forward_as_tuple(std::move(k)), std::forward_as_tuple(forward<Args>(args)...))`
3) If a key equivalent to `k` already exists in the container, does nothing. Otherwise, behaves like emplace_hint except that the element is constructed as `value_type(std::piecewise_construct, std::forward_as_tuple(k), std::forward_as_tuple(forward<Args>(args)...))`
4) If a key equivalent to `k` already exists in the container, does nothing. Otherwise, behaves like emplace_hint except that the element is constructed as `value_type(std::piecewise_construct, std::forward_as_tuple(std::move(k)), std::forward_as_tuple(forward<Args>(args)...))`

If rehashing occurs due to the insertion, all iterators are invalidated. Otherwise iterators are not affected. References are not invalidated. Rehashing occurs only if the new number of elements is greater than max_load_factor()*bucket_count().

### [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,2) Same as for emplace
3,4) Same as for emplace_hint

### [edit] Complexity

1,2) Same as for emplace
3,4) Same as for emplace_hint

### [edit] Notes

Unlike insert or emplace, these functions do not steal from move-only arguments if the insertion does not happen, which makes it easy to manipulate maps whose values are move-only types, such as `std::unordered_map<std::string, std::unique_ptr<foo>>`. In addition, `try_emplace` treats the key and the arguments to the `mapped_type` separately, unlike emplace, which requires the arguments to construct a `value_type` (that is, a `std::pair`)

### [edit] Example

| This section is incomplete Reason: no example

### [edit] See also

emplace | constructs element in-place (public member function) [edit]
emplace_hint | constructs elements in-place using a hint (public member function) [edit]
insert | inserts elements (public member function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/container/unordered_map/try_emplace&oldid=74455"
Category:

- Todo no example