Namespaces
Variants

std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>:: insert

From cppreference.net
定义于头文件 <unordered_map>
iterator insert ( const value_type & value ) ;
(1) (C++11 起)
iterator insert ( value_type && value ) ;
(2) (C++17 起)
template < class P >
iterator 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 起)
iterator 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 时参与重载决议。
7) 插入来自范围 [ first , last ) 的元素。
如果 [ first , last ) 不是有效的 范围 ,或者 first 和/或 last 是指向 * this 的迭代器,则行为未定义。
8) 从初始化列表 ilist 插入元素。
9) nh 为空 节点句柄 ,则不执行任何操作。否则,将 nh 所拥有的元素插入容器,并返回指向被插入元素的迭代器。若 nh 非空且 get_allocator() != nh.get_allocator() ,则行为未定义。
10) nh 为空 节点句柄 ,则不执行任何操作并返回结束迭代器。否则,将 nh 所拥有的元素插入容器,并返回指向键等价于 nh. key ( ) 的元素的迭代器。 hint 作为搜索起始位置的非强制性建议。若 nh 非空且 get_allocator ( ) ! = nh. get_allocator ( ) ,则行为未定义。

如果操作后新元素数量大于原 max_load_factor() * bucket_count() 将发生重哈希。
若发生重哈希(因插入导致),所有迭代器均失效。否则(未发生重哈希),迭代器保持有效。 若插入成功,通过节点句柄获取的元素指针和引用将失效,而该元素被提取前获得的指针和引用将变为有效。 (C++17 起)

目录

参数

hint - 迭代器,作为插入内容的建议位置
value - 要插入的元素值
first, last - 定义要插入元素来源 范围 的迭代器对
ilist - 要从中插入值的初始化列表
nh - 兼容的 节点句柄
类型要求
-
InputIt 必须满足 LegacyInputIterator 的要求。

返回值

1-6) 指向被插入元素的迭代器。
7,8) (无)
9,10) nh 为空则返回结束迭代器,否则返回指向被插入元素的迭代器。

异常

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())

示例

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

问题报告 应用于 发布时的行为 正确行为
LWG 2005 C++11 重载 (3,6) 仅在 P 可隐式转换为 value_type
参与重载决议
仅在 value_type
可从 P&& 构造时参与

参见

td> 使用提示原地构造元素
(公开成员函数)
原地构造元素
(公开成员函数)
创建从参数推断类型的 std::insert_iterator
(函数模板)