unordered_map和unordered_set的使用

目录

1. unordered_set系列的使用

1.1 unordered_map和unordered_set参考文档

1.2 unordered_set类的介绍 

1.3 unordered set和set的使用差异

1.4 unorder_map和map的使用差异

1.5 unordered_multimap/unordered_multiset

2. unordered_map和unordered_set代码


1. unordered_set系列的使用

1.1 unordered_map和unordered_set参考文档

unordered_map - C++ Referencehttps://legacy.cplusplus.com/reference/unordered_map/unordered_map/?kw=unordered_mapunordered_set - C++ Referencehttps://legacy.cplusplus.com/reference/unordered_set/unordered_set/?kw=unordered_setunordered_map和unordered_set跟map和set是高度相似的,它们的底层是哈希表,它们是C++11增加进来的,C++11之前只有map和set,但是后面决定哈希的系列效率更高一些,所以到C++11就i把它们引入进来了,引入进来的话取名字就犯了难,现在叫哈希map和哈希set?总感觉不是平行关系,像父子关系,所以就加了一个unordered,这个是无序的意思,unordered_map和unordered_set,map和set,最大的区别就是遍历的时候是无序的。

1.2 unordered_set类的介绍 

  • unordered_set的声明如下,Key就是unordered set底层关键字的类型。
  •  unordered set默认要求Key支持转换为整形,如果不支持或者想按自己的需求走可以自行实现支持将Key转成整形的仿函数传给第二个模板参数。
  • unordered set默认要求Key支持比较相等,如果不支持或者想按自己的需求走可以自行实现支持将Key比较相等的仿函数传给第三个模板参数。
  • unordered_set底层存储数据的内存是从空间配置器申请的,如果需要可以自己实现内存池,传给第四个参数。
  • 一般情况下,我们都不需要传后三个模板参数。
  • unordered _set底层是用哈希桶实现,增删查平均效率是O(1),迭代器遍历不再有序,为了跟set区分,所以取名unordered_set。
template < class Key, 
//unordered_set::key_type/value_type
    class Hash = hash<Key>, // unordered_set::hasher
    class Pred = equal_to<Key>, // unordered_set::key_equal
    class Alloc = allocator<Key> // unordered_set::allocator_type
    > class unordered_set;

1.3 unordered set和set的使用差异

  • unordered_set的支持增删查且跟set的使用一一样。
  • unordered_set和set的第一个差异是对key的要求不同,set要求Key支持小于比较,而
    unordered set要求Key支持转成整形且支持等于比较。
  • unordered set和set的第二个差异是迭代器的差异,set的iterator是双向迭代器,unordered_set是单向迭代器,其次set底层是红黑树,红黑树是二叉搜索树,走中序遍历是有序的,所以set迭代器遍历是有序+去重。而unordered set底层是哈希表,迭代器遍历是无序+去重。
  • unordered set和set的第三个差异是性能的差异,整体而言大多数场景下,unordered set的增删查改更快一些,因为红黑树增删查改效率是O(logN),而哈希表增删查平均效率是O(1)。
pair<iterator,bool> insert ( const value_type& val );
size_type erase ( const key_type& k );
iterator find ( const key_type& k );
#define _CRT_SECURE_NO_WARNINGS 1
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <iostream>
using namespace std;
void test_set2()
{
	const size_t N = 10000000;
	unordered_set<int> us;
	set<int> s;

	vector<int> v;
	v.reserve(N);
	srand(time(nullptr));
	for (size_t i = 0; i < N; i++)
	{
		//v.push_back(rand());//N比较大时,重复值较多
		//v.push_back(rand() + i);//重复值较少
		v.push_back(i);//没有重复值,有序的
	}
	//插入值到set
	size_t begin1 = clock();
	for (const auto& e : v)
	{
		s.insert(e);
	}
	size_t end1 = clock();
	cout << "set insert:" << end1 - begin1 << endl;
	//插入值到unordered_set
	size_t begin2 = clock();
	//us.reserve(N);
	for (const auto& e : v)
	{
		us.insert(e);
	}
	size_t end2 = clock();
	cout << "unordered_set insert:" << end2 - begin2 << endl;


	//统计查找到的次数
	int m1 = 0;
	size_t begin3 = clock();
	for (const auto& e : v)
	{
		auto ret = s.find(e);
		if (ret != s.end())
		{
			++m1;
		}
	}
	size_t end3 = clock();
	cout << "set find:" << end3 - begin3 << "->" << m1 << endl;

	int m2 = 0;
	size_t begin4 = clock();
	for (const auto& e : v)
	{
		auto ret = us.find(e);
		if (ret != us.end())
		{
			++m2;
		}
	}
	size_t end4 = clock();
	cout << "unordered_set find:" << end4 - begin4 << "->" << m2 << endl;
	cout << "set插入数据个数:" << s.size() << endl;
	cout << "unordered_set插入数据个数:" << us.size() << endl;

	size_t begin5 = clock();
	for (const auto& e : v)
	{
		s.erase(e);
	}
	size_t end5 = clock();
	cout << "set erase:" << end5 - begin5 << endl;

	size_t begin6 = clock();
	for (const auto& e : v)
	{
		us.erase(e);
	}
	size_t end6 = clock();
	cout << "unordered_set erase:" << end6 - begin6 << endl;

}
int main()
{
	//set<int> s = { 5,2,6,1,9,10,2,3,1,1 };
	//unordered_set<int> s = { 5,2,6,1,9,10,2,3,1,1 };//去重
	//迭代器遍历
	/*set<int>::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;*/
	//范围for遍历
	/*for (const auto& e : s)
	{
		cout << e << " ";
	}
	cout << endl;*/
	test_set2();
	return 0;
}

整体效率unorder系列还是全面领先,如果不需要排序的话还是建议使用unorder系列。

1.4 unorder_map和map的使用差异

  •  查看文档我们会发现unordered map的支持增删查改且跟map的使用一模一样。
  • unordered_map和map的第一个差异是对key的要求不同,map要求Key支持小于比较,而unordered map要求Key支持转成整形且支持等于比较,要理解unordered map的这个两点要求得后续我们结合哈希表底层实现才能真正理解,也就是说这本质是哈希表的要求。
  • unordered map和map的第二个差异是迭代器的差异,map的iterator是双向迭代器,
    unordered_map是单向迭代器,其次map底层是红黑树,红黑树是二叉搜索树,走中序遍历是有序的,所以map迭代器遍历是Key有序+去重。而unordered map底层是哈希表,迭代器遍历是Key无序+去重。
  • unordered map和map的第三个差异是性能的差异,整体而言大多数场景下,unordered map的增删查改更快一些,因为红黑树增删查改效率是 O(l0gN),而哈希表增删查平均效率是 O(1)。
pair<iterator,bool> insert ( const value_type& val );
size_type erase ( const key_type& k );
iterator find ( const key_type& k );
mapped_type& operator[] ( const key_type& k );
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
	//统计水果次数
	string arr[] = { "苹果","西瓜","苹果","西瓜","苹果","苹果","西瓜",
	"苹果","香蕉","苹果","香蕉" };
	//unordered_map<string, int> countMap;
	map<string, int> countMap;
	for (const auto& str : arr)
	{
		countMap[str]++;
	}
	for (const auto& e : countMap)
	{
		cout << e.first << ":" << e.second << endl;
	}
	cout << endl;
	return 0;
}

1.5 unordered_multimap/unordered_multiset

  • unordered multimap/unordered multiset跟multimap/multiset功能完全类似,支持Key冗余。 
  • unordered_multimap/unordered_multiset跟multimap/multiset的差异也是三个方面的差异,key的要求的差异,iterator及遍历顺序的差异,性能的差异。

2. unordered_map和unordered_set代码

C Plus Plus: C++program code - Gitee.comhttps://gitee.com/Axurea/c-plus-plus/tree/master/2025_01_27_unordered_map_unordered_set

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值