std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: try_emplace
|
template
<
class
...
Args
>
std:: pair < iterator, bool > try_emplace ( const Key & k, Args && ... args ) ; |
(1) | (C++17 起) |
|
template
<
class
...
Args
>
std:: pair < iterator, bool > try_emplace ( Key && k, Args && ... args ) ; |
(2) | (C++17 起) |
|
template
<
class
K,
class
...
Args
>
std:: pair < iterator, bool > try_emplace ( K && k, Args && ... args ) ; |
(3) | (C++26 起) |
|
template
<
class
...
Args
>
iterator try_emplace ( const_iterator hint, const Key & k, Args && ... args ) ; |
(4) | (C++17 起) |
|
template
<
class
...
Args
>
iterator try_emplace ( const_iterator hint, Key && k, Args && ... args ) ; |
(5) | (C++17 起) |
|
template
<
class
K,
class
...
Args
>
iterator try_emplace ( const_iterator hint, K && k, Args && ... args ) ; |
(6) | (C++26 起) |
如果容器中已存在与 k 等效的键,则不执行任何操作。否则,将一个新元素插入容器,该元素的键为 k ,值由 args 构造。在此情况下:
emplace
,区别在于元素被构造为
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
k
)
,
emplace
,区别在于元素被构造为
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
std
::
move
(
k
)
)
,
emplace
,区别在于元素被构造为
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
std::
forward
<
K
>
(
k
)
)
,
emplace_hint
,不同之处在于元素被构造为
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
k
)
,
emplace_hint
,不同之处在于元素被构造为
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
std
::
move
(
k
)
)
,
emplace_hint
,区别在于元素被构造为
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
std::
forward
<
K
>
(
k
)
)
,
- std:: is_convertible_v < K && , const_iterator > 与 std:: is_convertible_v < K && , iterator > 均为 false 。
- Hash :: is_transparent 与 KeyEqual :: is_transparent 均有效且各自表示一个类型。
如果在操作后新元素数量大于旧的
max_load_factor()
*
bucket_count()
将发生重新哈希。
如果发生重新哈希(由于插入操作),所有迭代器将失效。否则(未发生重新哈希),迭代器不会失效。
目录 |
参数
| k | - | 用于查找且在未找到时插入的键 |
| hint | - | 指向新元素将插入位置之前的迭代器 |
| args | - | 要转发给元素构造函数的参数 |
返回值
复杂度
注解
与
insert
或
emplace
不同,这些函数在插入未发生时不会从右值参数移动,这使得操作值为仅移动类型的映射(例如
std::
unordered_map
<
std::
string
,
std::
unique_ptr
<
foo
>>
)更为简便。此外,
try_emplace
将键和
mapped_type
的参数分开处理,这与需要构造
value_type
(即
std::pair
)参数的
emplace
不同。
重载版本
(
3
)
和
(
6
)
可以在不构造
Key
类型对象的情况下调用。
| 功能测试 宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__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) | 有序 和 无序 关联式 容器 中剩余成员函数的异构重载。重载版本 ( 3 ) 和 ( 6 ) 。 |
示例
#include <iostream> #include <string> #include <unordered_map> #include <utility> void print_node(const auto& node) { std::cout << '[' << node.first << "] = " << node.second << '\n'; } void print_result(auto const& pair) { std::cout << (pair.second ? "inserted: " : "ignored: "); print_node(*pair.first); } int main() { using namespace 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 (const auto& p : m) print_node(p); }
可能的输出:
inserted: [a] = a inserted: [b] = abcd inserted: [c] = cccccccccc ignored: [c] = cccccccccc [a] = a [b] = abcd [c] = cccccccccc
参阅
|
原位构造元素
(公开成员函数) |
|
|
使用提示原位构造元素
(公开成员函数) |
|
|
插入元素
或节点
(C++17 起)
(公开成员函数) |