【C++】-- STL之unordered_map/unordered_set详解(一)

简介: 【C++】-- STL之unordered_map/unordered_set详解

一、map/set和unordered_map/unordered_set的区别

STL有两种容器:序列式容器和关联式容器,序列式容器vetor/lost/deque,用来存储数据。关联式容器map/set/unordered_map/unordered_set用来存储数据+查找数据。

unordered_map和unordered_set是c++里面两个提供哈希表的容器,map和set底层是红黑树,unordered_map和unordered_set的底层是哈希表(散列表),是一种映射。

对于set和unordered_set的增删查,在10000个数据、100000个数据、1000000个数据的情况下分别作了对比:

1. #include<iostream>
2. #include<vector>
3. #include<set>
4. #include<time.h>
5. #include<unordered_set>
6. using namespace std;
7. 
8. void test_unordered_set()
9. {
10.   vector<int> v;
11.   v.reserve(10000);//100000、1000000
12.   srand((unsigned int)time(NULL));
13. 
14.   for (int i = 0; i < 10000; i++)//100000、1000000
15.   {
16.     v.push_back(rand());
17.   }
18. 
19.   //插入
20.   set<int> s;
21.   size_t begin1 = clock();
22.   for (auto e : v)
23.   {
24.     s.insert(e);
25.   }
26.   size_t end1 = clock();
27. 
28.   unordered_set<int> us;
29.   size_t begin2 = clock();
30.   for (auto e : v)
31.   {
32.     us.insert(e);
33.   }
34.   size_t end2 = clock();
35. 
36.   cout << "set insert time:" << end1 - begin1 << endl;
37.   cout << "unorder_set insert time:" << end2 - begin2 << endl;
38. 
39.   //查找
40.   size_t begin3 = clock();
41.   for (auto e : v)
42.   {
43.     s.find(e);//set自带的查找效率是O(logn)
44.   }
45.   size_t end3 = clock();
46. 
47.   size_t begin4 = clock();
48.   for (auto e : v)
49.   {
50.     us.find(e); //unordered_set自带的查找,优点:使用哈希特性查找,效率高--O(1)
51.   }
52.   size_t end4 = clock();
53. 
54.   cout << "set find time:" << end3 - begin3 << endl;
55.   cout << "unorder_set find time:" << end4 - begin4 << endl;
56. 
57.   //删除
58.   size_t begin5 = clock();
59.   for (auto e : v)
60.   {
61.     s.erase(e);
62.   }
63.   size_t end5 = clock();
64. 
65.   size_t begin6 = clock();
66.   for (auto e : v)
67.   {
68.     us.erase(e);
69.   }
70.   size_t end6 = clock();
71. 
72.   cout << "set erase time:" << end5 - begin5 << endl;
73.   cout << "unorder_set erase time:" << end6 - begin6 << endl;
74. }
75. 
76. int main()
77. {
78. 
79.   test_unordered_set();
80. 
81.   return 0;
82. }

10000个数据的时间:

100000个数据的时间:

1000000个数据的时间:

 

可以看到,当数据量越大时,unordered_set相比于set所消耗的时间越少,这是因为unordered_set的底层是哈希表,增删查的效率更高。

二、 unordered_set

1.特点

(1) unordered_map是存储<value, value>键值对的关联式容器,对value进行快速索引。

(2)在unordered_set中,元素的值同时是其键,是唯一标识,键和映射值的类型相同,键不可修改。unordered_set中的元素在容器不可修改,但是可以插入和删除元素。

(3)unordered_set中的元素不按任何特定顺序排序,而是根据其哈希值组织到存储桶中,允许直接根据value快速访问各个元素(平均时间复杂度是一定的)。

(4)unordered_set比set通过键访问单个元素的速度更快,但它通常在遍历元素子集的范围迭代方面效率较低。

(5)容器中的迭代器至少有正向迭代器。

2.构造

有以下几种构造方式:

1. explicit unordered_set ( size_type n = /* see below */,
2. const hasher& hf = hasher(),
3. const key_equal& eql = key_equal(),
4. const allocator_type& alloc = allocator_type() );//构造空的unordered_set对象
5. 
6. template <class InputIterator>
7. unordered_set ( InputIterator first, InputIterator last,
8.                          size_type n = /* see below */,
9. const hasher& hf = hasher(),
10. const key_equal& eql = key_equal(),
11. const allocator_type& alloc = allocator_type() );//用迭代器范围构造unordered_set对象
12. 
13. unordered_set ( const unordered_set& ust );//拷贝构造一个unordered_set对象

(1)构造一个空的 unordered_set对象

unordered_set<int> us1;

向里面插入元素:

1.  us1.insert(2);
2.  us1.insert(72);
3.  us1.insert(6);
4.  us1.insert(35);
5.  us1.insert(291);
6.  us1.insert(327);

(2) 用迭代器范围构造unordered_set对象

用us1的迭代器范围构造us2:

unordered_set<int> us2(us1.begin(), us1.end());

(3) 拷贝构造一个unordered_set对象

用us2拷贝构造us3:

unordered_set<int> us3(us2);

3.容量

(1)empty( )

判断unordered_set是否为空:

cout << us3.max_size() << endl;

判断us3是否为空:

cout << us3.empty() << endl;//判断us3是否为空

不为空:

(2)size( )

返回unordered_set中的元素个数

size_type size() const noexcept;

求us3中的元素个数:

cout << us3.size() << endl;

(3)max_size( )

返回unordered_set可存储的最大元素个数:

size_type max_size() const noexcept;

求us3最大元素个数 :

cout << us3.max_size() << endl;

 

 

4.迭代器

(1)begin( )

返回迭代器开始:

iterator begin() noexcept;

返回us3迭代器开始:

unordered_set<int>::iterator it = us3.begin();

(2)end( )

返回迭代器结尾:

iterator end() noexcept;

返回us3迭代器结尾:

us3.end();

 

5.查找

(1)find( )

如果找到元素就返回元素所在位置,否则返回元素结尾:

iterator find ( const key_type& k );

在us3中查找327:

1.  unordered_set<int>::iterator ret = us3.find(327);
2.  if (ret != us3.end())
3.  {
4.    cout << "找到了" << endl;
5.  }
6.  else
7.  {
8.    cout << "没找到" << endl;
9.  }

(2)count( )

统计容器中值为k的元素的个数:

size_type count ( const key_type& k ) const;

统计us3中值为291的元素的个数:

cout << us3.count(291) << endl;

 

相关文章
|
2天前
|
存储 搜索推荐 C++
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
|
2天前
|
设计模式 C语言 C++
【C++进阶(六)】STL大法--栈和队列深度剖析&优先级队列&适配器原理
【C++进阶(六)】STL大法--栈和队列深度剖析&优先级队列&适配器原理
|
2天前
|
存储 缓存 编译器
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
|
2天前
|
算法 C++ 容器
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
|
2天前
|
编译器 C++
【C++进阶(三)】STL大法--vector迭代器失效&深浅拷贝问题剖析
【C++进阶(三)】STL大法--vector迭代器失效&深浅拷贝问题剖析
|
3天前
|
存储 算法 C语言
c++的学习之路:9、STL简介与string(1)
c++的学习之路:9、STL简介与string(1)
20 0
|
1月前
|
存储 自然语言处理 C++
map和set的简单介绍
map和set的简单介绍
20 1
|
1月前
|
存储 安全 Java
java集合框架及其特点(List、Set、Queue、Map)
java集合框架及其特点(List、Set、Queue、Map)
|
3月前
|
JavaScript 前端开发 定位技术
JavaScript 中如何代理 Set(集合) 和 Map(映射)
JavaScript 中如何代理 Set(集合) 和 Map(映射)
50 0
|
3月前
|
存储 安全 Java
Map和Set(JAVA)
Map和Set(JAVA)
50 1