std::unordered_map::try_emplace
STD:无序[医]地图::尝试[医]座落
template | (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%29如果密钥相当于k已经存在于容器中,什么都不做。否则,行为就像座落但元素构造为value_type(std::piecewise_construct, std::forward_as_tuple(k), std::forward_as_tuple(forward<Args>(args)...))
2%29如果密钥相当于k已经存在于容器中,什么都不做。否则,行为就像座落但元素构造为value_type(std::piecewise_construct, std::forward_as_tuple(std::move(k)), std::forward_as_tuple(forward<Args>(args)...))
3%29如果密钥相当于k已经存在于容器中,什么都不做。否则,行为就像座落[医]暗示但元素构造为value_type(std::piecewise_construct, std::forward_as_tuple(k), std::forward_as_tuple(forward<Args>(args)...))
4%29如果密钥相当于k已经存在于容器中,什么都不做。否则,行为就像座落[医]暗示但元素构造为value_type(std::piecewise_construct, std::forward_as_tuple(std::move(k)), std::forward_as_tuple(forward<Args>(args)...))
如果由于插入而发生重散列,则所有迭代器都将失效。否则迭代器不会受到影响。引用不失效。只有当新元素数大于max_load_factor()*bucket_count()
...
参数
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 |
返回值
1,2%29与座落
3.4%29与座落[医]暗示
复杂性
1,2%29与座落
3.4%29与座落[医]暗示
注记
不像插入或座落,如果不进行插入,则这些函数不会从rvalue参数中移动,这使得操作其值为仅移动类型的映射变得容易,例如std::unordered_map<std::string, std::unique_ptr<foo>>.此外,try_emplace对象的键和参数。mapped_type分别,不像座落,它需要参数来构造value_type%28,即astd::pair29%。
例
二次
#include <iostream>
#include <utility>
#include <string>
#include <unordered_map>
int main()
{
using namespace std::literals;
std::unordered_map<std::string, std::string> m;
m.try_emplace("a", "a"s
m.try_emplace("b", "abcd"
m.try_emplace("c", 10, 'c'
for (const auto &p : m) {
std::cout << p.first << " => " << p.second << '\n';
}
}
二次
可能的产出:
二次
a => a
b => abcd
c => cccccccccc
二次
另见
emplace | constructs element in-place (public member function) |
---|---|
emplace_hint | constructs elements in-place using a hint (public member function) |
insert | inserts elements or nodes (since C++17) (public member function) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
http://en.cppreference.com/w/cpp/container/unorder[医]地图/尝试[医]座落