欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 【STL】unordered_set

【STL】unordered_set

2025/4/23 23:57:31 来源:https://blog.csdn.net/jmlangel_/article/details/147412856  浏览:    关键词:【STL】unordered_set

C C C++ 11 11 11 中, S T L STL STL 标准库引入了一个新的标准关联式容器 u n o r d e r e d _ s e t unordered\_set unordered_set无序集合)。功能和 s e t set set 类似,都用于存储唯一元素。但是其底层数据结构是哈希表,因此集合中的元素都是无序存储的,所以增删查的时间复杂度为 O ( 1 ) O(1) O(1),增删查的效率比 s e t set set 高。

文章目录

  • 一、unordered_set 的介绍
  • 二、unordered_set 的使用(常用接口)
    • 1. 常见构造
    • 2. iterator 的使用
    • 3. 增删查
    • 4. unordered_multiset
  • 三、unordered_set 的模拟实现
    • 1. STL 中的 hash_set 源码
    • 2. unordered_set 的迭代器
    • 3. 模拟实现 unordered_set
  • 总结


一、unordered_set 的介绍

前面部分我们已经详细介绍了 s e t set set 容器,可以参考我的这篇博客:【STL】 s e t set set。由于 s e t set set u n o r d e r e d _ s e t unordered\_set unordered_set 这两个容器只是底层实现结构不同,其功能高度相似,基本上只要掌握 s e t set set 的用法, u n o r d e r e d _ s e t unordered\_set unordered_set 也就会用了。因此,和 s e t set set 相比只有一些性能和使用的差异,这里只介绍其差异部分。

在这里插入图片描述

u n o r d e r e d _ s e t unordered\_set unordered_set 的声明如下:

template < class Key, 						// unordered_set::key_type/value_typeclass Hash = hash<Key>, 			// unordered_set::hasherclass Pred = equal_to<Key>, 		// unordered_set::key_equalclass Alloc = allocator<Key> 	// unordered_set::allocator_type> class unordered_set;
  1. K e y Key Key 就是 u n o r d e r e d _ s e t unordered\_set unordered_set 底层关键字的类型。

  2. u n o r d e r e d _ s e t unordered\_set unordered_set 默认要求 K e y Key Key 支持转换为整形,如果不支持或者有自己的需求可以自行实现支持将 K e y Key Key 转成整形的仿函数传给第二个模板参数。

  3. u n o r d e r e d _ s e t unordered\_set unordered_set 默认要求 K e y Key Key 支持比较相等,如果不支持或者有自己的需求可以自行实现支持将 K e y Key Key 比较相等的仿函数传给第三个模板参数。

  4. u n o r d e r e d _ s e t unordered\_set unordered_set 底层存储数据的内存是从空间配置器申请的,如果需要可以自己实现内存池,传给第四个参数。

注意:一般情况下,我们都不需要传后三个模板参数。

u n o r d e r e d _ s e t unordered\_set unordered_set 底层是用哈希桶实现,增删查平均效率是 O ( 1 ) O(1) O(1),迭代器遍历不再有序,为了跟 s e t set set 区分,所以取名 u n o r d e r e d _ s e t unordered\_set unordered_set无序集合)。


二、unordered_set 的使用(常用接口)

u n o r d e r e d _ s e t unordered\_set unordered_set 的底层结构是哈希表,因此不支持比较排序,所以细节上根据这一点和 s e t set set 有略微不同,其他都完全类似。这里只给出常用接口,更多详细信息可以自行查官方文档: u n o r d e r e d _ s e t unordered\_set unordered_set

1. 常见构造

构造 ( c o n s t r u c t o r ) (constructor) (constructor) 函数声明接口说明
u n o r d e r e d _ s e t ( ) unordered\_set() unordered_set()无参默认构造
u n o r d e r e d _ s e t ( c o n s t u n o r d e r e d _ s e t & u s t ) unordered\_set(const\ unordered\_set\&\ ust) unordered_set(const unordered_set& ust)拷贝构造
u n o r d e r e d _ s e t ( I n p u t I t e r a t o r f i r s t , I n p u t I t e r a t o r l a s t ) unordered\_set(InputIterator\ first, InputIterator\ last) unordered_set(InputIterator first,InputIterator last)使用迭代器区间构造
u n o r d e r e d _ s e t ( i n i t i a l i z e r _ l i s t < v a l u e _ t y p e > i l ) unordered\_set (initializer\_list<value\_type> il) unordered_set(initializer_list<value_type>il)使用 i n i t i a l i z e r initializer initializer 列表构造

2. iterator 的使用

i t e r a t o r iterator iterator 的使用接口说明
b e g i n ( ) begin() begin() + + + e n d ( ) end() end() i t e r a t o r iterator iterator
c b e g i n ( ) cbegin() cbegin() + + + c e n d ( ) cend() cend() c o n s t _ i t e r a t o r const\_iterator const_iterator

u n o r d e r e d _ s e t unordered\_set unordered_set 的迭代器是一个单向迭代器iterator -> a forward iterator to const value_type

在这里插入图片描述

3. 增删查

u n o r d e r e d _ s e t unordered\_set unordered_set 增删查接口说明
i n s e r t insert insert插入 v a l val val 数据
e r a s e erase erase删除 v a l val val 数据
f i n d find find查找 v a l val val,返回 v a l val val 位置的迭代器(没找到返回 e n d ( ) end() end()
c o u n t count count查找 v a l val val,返回 v a l val val 的个数

由于 u n o r d e r e d _ s e t unordered\_set unordered_set 不支持比较大小,且容器内元素是无序的,因此就没有 l o w e r _ b o u n d lower\_bound lower_bound u p p e r _ b o u n d upper\_bound upper_bound 接口了。

4. unordered_multiset

u n o r d e r e d _ m u l t i s e t unordered\_multiset unordered_multiset m u l t i s e t multiset multiset 的使用基本完全类似,都支持关键值( K e y Key Key)冗余

m u l t i s e t multiset multiset 完全类似, i n s e r t / f i n d / c o u n t / e r a s e insert/find/count/erase insert/find/count/erase 都围绕着支持值冗余有所差异:

  1. i n s e r t insert insert 可以插入相同的值

  2. 如果要查找的 x x x 有多个值, f i n d find find 会返回第一个迭代器

  3. c o u n t count count 会返回 x x x 的实际个数。

  4. e r a s e erase erase 指定值删除时,会删除所有的 x x x


三、unordered_set 的模拟实现

1. STL 中的 hash_set 源码

S G I − S T L 30 SGI-STL\ 30 SGISTL 30 版本是 C C C++ 11 11 11 之前的 S T L STL STL 版本,源代码中没有 u n o r d e r e d _ s e t unordered\_set unordered_set,因为这个容器是 C C C++ 11 11 11 之后才更新的。但是 S G I − S T L 30 SGI-STL\ 30 SGISTL 30 实现了哈希表,容器的名字是 h a s h _ s e t hash\_set hash_set,它是作为非标准容器(非 C C C++ 标准规定必须实现的容器)出现的。

  1. h a s h _ s e t hash\_set hash_set

  1. h a s h t a b l e . h hashtable.h hashtable.h

2. unordered_set 的迭代器


3. 模拟实现 unordered_set

  1. h a s h t a b l e . h hashtable.h hashtable.h

  1. u n o r d e r e d _ s e t . h unordered\_set.h unordered_set.h

  1. t e s t . c p p test.cpp test.cpp


总结

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词