std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::insert
来自 cppreference.cn
< cpp | container | unordered map
std::pair<iterator, bool> insert( const value_type& value ); |
(1) | (自 C++11 起) |
std::pair<iterator, bool> insert( value_type&& value ); |
(2) | (自 C++17 起) |
template< class P > std::pair<iterator, bool> insert( P&& value ); |
(3) | (自 C++11 起) |
iterator insert( const_iterator hint, const value_type& value ); |
(4) | (自 C++11 起) |
iterator insert( const_iterator hint, value_type&& value ); |
(5) | (自 C++17 起) |
template< class P > iterator insert( const_iterator hint, P&& value ); |
(6) | (自 C++11 起) |
template< class InputIt > void insert( InputIt first, InputIt last ); |
(7) | (自 C++11 起) |
void insert( std::initializer_list<value_type> ilist ); |
(8) | (自 C++11 起) |
insert_return_type insert( node_type&& nh ); |
(9) | (自 C++17 起) |
iterator insert( const_iterator hint, node_type&& nh ); |
(10) | (自 C++17 起) |
将元素插入到容器中,如果容器尚未包含具有等效键的元素。
1-3) 插入 value。
重载 (3) 等效于 emplace(std::forward<P>(value)) 并且仅在 std::is_constructible<value_type, P&&>::value == true 时参与重载解析。
4-6) 插入 value,使用 hint 作为非约束性建议,指示搜索应从何处开始。
重载 (6) 等效于 emplace_hint(hint, std::forward<P>(value)) 并且仅在 std::is_constructible<value_type, P&&>::value == true 时参与重载解析。
9) 如果 nh 是空节点句柄,则不执行任何操作。否则,如果容器尚未包含具有与 nh.key() 等效的键的元素,则将 nh 拥有的元素插入到容器中。如果 nh 不为空且 get_allocator() != nh.get_allocator(),则行为未定义。
10) 如果 nh 是空节点句柄,则不执行任何操作并返回结束迭代器。否则,如果容器尚未包含具有与 nh.key() 等效的键的元素,则将 nh 拥有的元素插入到容器中,并返回指向具有与 nh.key() 等效的键的元素的迭代器(无论插入成功还是失败)。如果插入成功,则从 nh 移动,否则它保留元素的所有权。hint 用作非约束性建议,指示搜索应从何处开始。如果 nh 不为空且 get_allocator() != nh.get_allocator(),则行为未定义。
如果在操作之后,新元素数量大于旧元素数量 max_load_factor()
*
bucket_count()
,则会发生重哈希。
如果发生重哈希(由于插入),则所有迭代器都将失效。否则(不重哈希),迭代器不会失效。如果插入成功,则在节点句柄中持有时获得的元素的指针和引用将失效,并且在该元素被提取之前获得的指针和引用将变为有效。(自 C++17 起)
目录 |
[编辑] 参数
hint | - | 迭代器,用作关于在何处插入内容的建议 |
value | - | 要插入的元素值 |
first, last | - | 定义要插入元素的源范围的迭代器对 |
ilist | - | 要从中插入值的初始化器列表 |
nh | - | 兼容的节点句柄 |
类型要求 | ||
-InputIt 必须满足 LegacyInputIterator 的要求。 |
[编辑] 返回值
1-3) 一个对,包含指向插入元素的迭代器(或指向阻止插入的元素的迭代器)和一个 bool 值,该值设置为 true 当且仅当发生插入时。
4-6) 指向插入元素的迭代器,或指向阻止插入的元素的迭代器。
7,8) (无)
9)
insert_return_type
类型的对象,其成员初始化如下- 如果 nh 为空,则
inserted
为 false,position
为 end(),并且node
为空。 - 否则,如果发生插入,则
inserted
为 true,position
指向插入的元素,并且node
为空。 - 如果插入失败,则
inserted
为 false,node
具有 nh 的先前值,并且position
指向具有与 nh.key() 等效的键的元素。
10) 如果 nh 为空,则为结束迭代器;如果发生插入,则为指向插入元素的迭代器;如果插入失败,则为指向具有与 nh.key() 等效的键的元素的迭代器。
[编辑] 异常
1-6) 如果由于任何原因抛出异常,这些函数都不会产生任何影响(强异常安全保证)。
7,8) 无异常安全保证。
9,10) 如果由于任何原因抛出异常,这些函数都不会产生任何影响(强异常安全保证)。
[编辑] 复杂度
1-6) 平均情况:
O(1)
,最坏情况 O(size())
。7,8) 平均情况:
O(N)
,其中 N 是要插入的元素数量。最坏情况:O(N * size() + N)
。9,10) 平均情况:
O(1)
,最坏情况 O(size())
。[编辑] 注意
提示插入 (4-6) 不返回布尔值,以便与顺序容器(例如 std::vector::insert)上的位置插入在签名上兼容。这使得创建通用插入器(例如 std::inserter)成为可能。检查提示插入是否成功的一种方法是在前后比较 size()
。
[编辑] 示例
运行此代码
#include <iostream> #include <string> #include <unordered_map> int main () { std::unordered_map<int, std::string> dict = {{1, "one"}, {2, "two"}}; dict.insert({3, "three"}); dict.insert(std::make_pair(4, "four")); dict.insert({{4, "another four"}, {5, "five"}}); const bool ok = dict.insert({1, "another one"}).second; std::cout << "inserting 1 => \"another one\" " << (ok ? "succeeded" : "failed") << '\n'; std::cout << "contents:\n"; for (auto& p : dict) std::cout << ' ' << p.first << " => " << p.second << '\n'; }
可能的输出
inserting 1 => "another one" failed contents: 5 => five 1 => one 2 => two 3 => three 4 => four
[编辑] 缺陷报告
以下行为更改缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2005 | C++11 | 重载 (3,6) 仅参与重载 解析,如果 P 可隐式转换为 value_type |
仅在 value_type 可从 P&& 构造时参与 |
[编辑] 参见
就地构造元素 (公共成员函数) | |
使用提示就地构造元素 (公共成员函数) | |
(C++17) |
插入元素或在键已存在时赋值给当前元素 (公共成员函数) |
创建从参数推断类型的 std::insert_iterator (函数模板) |