C++ STL 库 - <unordered_set>

创建于 2024-12-03 / 28
字体: [默认] [大] [更大]

简介

它是一个关联容器,以无特定顺序存储唯一元素,并允许根据其值快速检索单个元素。


定义

以下是 std::unordered_set 的定义

template < class Key,                        
           class Hash = hash<Key>,        
           class Pred = equal_to<Key>,    
           class Alloc = allocator<Key>   
           > class unordered_set;

参数

  • Key − 它定义了元素的类型。

  • Hash − 它是一元函数对象。

  • Pred − 它是一个二元谓词,它接受两个与元素类型相同的参数并返回一个布尔值。

  • Alloc − 它定义了允许的类型。


成员类型

以下成员类型可以用作成员函数的参数或返回类型。

成员类型 定义 注意事项
key_type It is the first template parameter(Key
value_type It is the first template parameter(Key defaults to key_type
hasher It is the second template parameter(Hash defaults to: hash<key_type>
key_equal It is the third template parameter (Pred) defaults to: equal_to<key_type>
allocator_type It is the fourth template parameter (Alloc) defaults to: allocator<value_type>
reference Alloc::reference
const_reference Alloc::const_reference
pointer Alloc::pointer for the default allocator: value_type*
const_pointer Alloc::const_pointer for the default allocator: const value_type*
iterator const value_type a forward iterator to * convertible to const_iterator
const_iterator const value_type a forward iterator to *
local_iterator const value_type a forward iterator to * convertible to const_local_iterator
const_local_iterator const value_type a forward iterator to *
size_type an unsigned integral type usually the same as size_t
difference_type a signed integral type usually the same as ptrdiff_t

成员函数

下面是成员函数列表

序号 方法 & 说明
1 (constructor)

它构造 unordered_set。

2 (destructor)

它破坏了 unordered_set。

3 operator=

它用于分配内容。


容量

序号 容量 & 说明
1 empty

用于检测容器是否为空。

2 size

它返回容器大小。

3 max_size

它返回最大尺寸。


迭代器

序号 迭代器 & 说明
1 begin

它将迭代器返回到开头。

2 end

它将迭代器返回到结束。

3 cbegin

它将 const_iterator 返回到开头。

4 cend

它返回 const_iterator 以结束。


元素查找

序号 元素查找 & 说明
1 find

它用于获取元素的迭代器。

2 count

它用于计算具有特定键的元素。

3 equal_range

它用于获取具有特定键的元素范围。


修饰符

序号 修饰符 & 说明
1 emplace

它用于构造和插入元素。

2 emplace_hint

它用于构造和插入带有提示的元素。

3 insert

它用于插入元素。

4 erase

它用于擦除元素。

5 clear

它用于清除内容。

6 swap

它用于交换内容。


序号 桶 & 说明
1 bucket_count

它返回桶的数量。

2 max_bucket_count

它返回最大桶数。

3 bucket_size

它返回桶大小。

4 bucket

它定位元素的桶。


哈希策略

序号 哈希策略 & 说明
1 load_factor

它返回负载系数。

2 max_load_factor

用于获取或设置最大负载系数。

3 rehash

用于设置桶数。

4 reserve

It gives a request to capacity chage of backets


观察者模式

序号 观察者模式 & 说明
1 hash_function

它用于获取哈希函数。

2 key_eq

它用于获取关键等价谓词。

3 get_allocator

它用于获取分配器。

序号 非成员函数重载 & 说明
1 operators (unordered_set)

它用于获取哈希函数。

2 swap (unordered_set)

它交换两个 unordered_set 容器的内容。


预定义的迭代器

序号 非成员函数重载 & 说明
1 operators (unordered_set)

它用于获取哈希函数。

2 swap (unordered_set)

它交换两个 unordered_set 容器的内容。



0 人点赞过