المتغيرات
فضاءات التسمية
أفعال

std::{{{1}}}::try_emplace

من cppreference.com
template <class... Args>
pair<iterator, bool> try_emplace(const key_type& k, Args&&... args);
(1) قالب:mark since c++17
template <class... Args>
pair<iterator, bool> try_emplace(key_type&& k, Args&&... args);
(2) قالب:mark since c++17
template <class... Args>
iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args);
(3) قالب:mark since c++17
template <class... Args>
iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args);
(4) قالب:mark since c++17
1) If the key ‎k‎ already exists in the container, does nothing. Otherwise, behaves like emplace except that the element is constructed as value_type(k, std::forward<Args>(args)...)
2) If the key ‎k‎ already exists in the container, does nothing. Otherwise, behaves like emplace except that the element is constructed as value_type(std::move(k), std::forward<Args>(args)...).
3) If the key ‎k‎ already exists in the container, does nothing. Otherwise, behaves like emplace_hint except that the element is constructed as value_type(k, std::forward<Args>(args)...)
4) If the key ‎k‎ already exists in the container, does nothing. Otherwise, behaves like emplace_hint except that the element is constructed as value_type(std::move(k), std::forward<Args>(args)...).

Note: the effect above may change to ‎value_type(std::piecewise_construct, std::forward_as_tuple(k), std::forward_as_tuple(forward<Args>(args)...))‎ in the next C++17 draft.

Information on iterator invalidation is copied from here

محتويات

[تعديل] 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

[تعديل] Return value

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

[تعديل] Complexity

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

[تعديل] 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::map<std::string, std::unique_ptr<foo>>‎. In addition, ‎try_emplace‎ treats the key and the arguments to the ‎mapped_type‎ separate, unlike emplace, which requires a the arguments to construct a ‎value_type‎ (that is, a ‎std::pair‎)

[تعديل] Example

[تعديل] See also

قالب:dsc mem funقالب:dsc mem funقالب:dsc mem fun