std::unordered_set<Key,Hash,KeyEqual,Allocator>:: insert
|
std::
pair
<
iterator,
bool
>
insert
(
const
value_type
&
value
)
;
|
(1) | (自 C++11 起) |
|
std::
pair
<
iterator,
bool
>
insert
(
value_type
&&
value
)
;
|
(2) | (自 C++11 起) |
|
iterator insert
(
const_iterator hint,
const
value_type
&
value
)
;
|
(3) | (自 C++11 起) |
|
iterator insert
(
const_iterator hint, value_type
&&
value
)
;
|
(4) | (自 C++11 起) |
|
template
<
class
InputIt
>
void insert ( InputIt first, InputIt last ) ; |
(5) | (自 C++11 起) |
|
void
insert
(
std::
initializer_list
<
value_type
>
ilist
)
;
|
(6) | (自 C++11 起) |
|
insert_return_type insert
(
node_type
&&
nh
)
;
|
(7) | (自 C++17 起) |
|
iterator insert
(
const_iterator hint, node_type
&&
nh
)
;
|
(8) | (自 C++17 起) |
|
template
<
class
K
>
std:: pair < iterator, bool > insert ( K && obj ) ; |
(9) | (自 C++23 起) |
|
template
<
class
K
>
iterator insert ( const_iterator hint, K && obj ) ; |
(10) | (自 C++23 起) |
如果容器中尚未包含具有等效键的元素,则将元素插入容器。
value_type
类型的对象
u
,并将其插入
*
this
。若
equal_range
(
u
)
!
=
hash_function
(
)
(
obj
)
||
contains
(
u
)
为
true
,则行为未定义。
value_type
必须能从
std::
forward
<
K
>
(
obj
)
可就位构造
到
unordered_set
中。此重载仅当
Hash
与
KeyEqual
均为
透明
时参与重载决议。这要求
Hash
可同时被
K
和
Key
类型调用,且
KeyEqual
为透明,共同使得无需构造
Key
实例即可调用此函数。
否则,使用
std::
forward
<
K
>
(
obj
)
构造
value_type
类型的对象
u
,随后将
u
插入
*
this
。
Template:hint
作为搜索起始位置的非强制性建议。若
equal_range
(
u
)
!
=
hash_function
(
)
(
obj
)
||
contains
(
u
)
为
true
,则行为未定义。
value_type
必须满足从
std::
forward
<
K
>
(
obj
)
到
unordered_set
的
EmplaceConstructible
要求。此重载仅当满足以下条件时参与重载决议:
- std:: is_convertible_v < K && , const_iterator > 与 std:: is_convertible_v < K && , iterator > 均为 false ,且
-
Hash
::
is_transparent
和
KeyEqual
::
is_transparent
合法且均表示类型(假定该
Hash可同时被K和Key类型调用,且KeyEqual具有透明性)
Key
实例即可调用此函数。
如果在操作后新元素数量大于旧的
max_load_factor()
*
bucket_count()
将发生重哈希。
如果发生重哈希(由于插入操作),所有迭代器都会失效。否则(未发生重哈希),迭代器不会失效。
如果插入成功,当元素保存在节点句柄中时获取的指向该元素的指针和引用会失效,而在该元素被提取之前获取的指针和引用将变为有效。
(C++17 起)
目录 |
参数
| hint | - | 迭代器,作为插入内容的建议位置 |
| value | - | 要插入的元素值 |
| first, last | - | 定义待插入元素源 范围 的迭代器对 |
| ilist | - | 待插入值的初始化列表 |
| nh | - | 兼容的 节点句柄 |
| obj | - | 可与键进行透明比较的任意类型值 |
| 类型要求 | ||
-
InputIt
必须满足
LegacyInputIterator
的要求。
|
||
返回值
insert_return_type
类型的对象,其成员按以下方式初始化:
-
如果
nh
为空,则
inserted为 false ,position为 end ( ) ,且node为空。 -
如果插入成功,则
inserted为 true ,position指向被插入的元素,且node为空。 -
如果插入失败,则
inserted为 false ,node保留 nh 先前的值,且position指向一个键与 nh. key ( ) 等效的元素。
异常
|
本节内容不完整
原因:案例5-10 |
复杂度
O(1)
,最坏情况
O(size())
。
O(N)
,其中 N 为待插入元素的数量。最坏情况:
O(N * size() + N)
。
O(1)
,最坏情况
O(size())
。
注释
提示插入
(
(
3,4
)
,
(
8
)
和
(
10
)
)
不返回布尔值,这是为了与顺序容器(如
std::vector::insert
)的位置插入操作保持签名兼容。这使得创建通用插入器(如
std::inserter
)成为可能。检查提示插入是否成功的一种方法是比较插入前后的
size()
。
| 功能测试 宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__cpp_lib_associative_heterogeneous_insertion
|
202311L
|
(C++26) | 为 有序 和 无序 关联 容器 中剩余成员函数提供异构重载 ( 9,10 ) |
示例
#include <array> #include <iostream> #include <unordered_set> std::ostream& operator<<(std::ostream& os, std::unordered_set<int> const& s) { for (os << '[' << s.size() << "] { "; int i : s) os << i << ' '; return os << "}\n"; } int main () { std::unordered_set<int> nums{2, 3, 4}; std::cout << "1) Initially: " << nums << std::boolalpha; auto p = nums.insert(1); // 插入元素,重载 (1) std::cout << "2) '1' was inserted: " << p.second << '\n'; std::cout << "3) After insertion: " << nums; nums.insert(p.first, 0); // 使用提示插入,重载 (3) std::cout << "4) After insertion: " << nums; std::array<int, 4> a = {10, 11, 12, 13}; nums.insert(a.begin(), a.end()); // 插入范围,重载 (5) std::cout << "5) After insertion: " << nums; nums.insert({20, 21, 22, 23}); // 插入初始化列表,重载 (6) std::cout << "6) After insertion: " << nums; std::unordered_set<int> other_nums = {42, 43}; auto node = other_nums.extract(other_nums.find(42)); nums.insert(std::move(node)); // 插入节点,重载 (7) std::cout << "7) After insertion: " << nums; node = other_nums.extract(other_nums.find(43)); nums.insert(nums.begin(), std::move(node)); // 使用提示插入节点,重载 (8) std::cout << "8) After insertion: " << nums; }
可能的输出:
1) Initially: [3] { 4 3 2 }
2) '1' was inserted: true
3) After insertion: [4] { 1 2 3 4 }
4) After insertion: [5] { 0 1 2 3 4 }
5) After insertion: [9] { 13 12 11 10 4 3 2 1 0 }
6) After insertion: [13] { 23 22 13 12 11 10 21 4 20 3 2 1 0 }
7) After insertion: [14] { 42 23 22 13 12 11 10 21 4 20 3 2 1 0 }
8) After insertion: [15] { 43 42 23 22 13 12 11 10 21 4 20 3 2 1 0 }
参见
|
原地构造元素
(公开成员函数) |
|
| td> 使用提示原地构造元素 | |
|
创建从参数推断类型的
std::insert_iterator
(函数模板) |