C++——哈希unordered_set/unordered_map的封装

news2024/9/23 15:29:02

目录

前言

二、unordered_set的封装

1.模板参数列表的改造

2. 增加迭代器操作

3. 模板参数的意义

三、unordered_map的封装 

1、“轮子所需要的参数

2、迭代器

四、完整代码

1、HashTable

2、unordered_set

 3、unordered_map

总结



前言

unordered_set和map的介绍在上一篇博客有所提到

C++——深部解析哈希

在编程的世界里,数据结构是构建高效、可扩展程序的基石。C++作为一门功能强大的编程语言,提供了丰富的标准库容器,其中unordered_set和map以其高效的查找和插入操作而广受欢迎。然而,尽管这些容器功能强大,直接使用它们有时可能不够直观或灵活,特别是在需要特定行为或扩展功能时。因此,封装这些容器,以提供更符合特定需求的接口,成为了一种常见的编程实践。
本博客旨在深入探讨如何封装C++中的unordered_set和map,以创建更加灵活、易用的数据结构。通过实例和代码演示,你将学会如何扩展这些容器的功能,以满足项目中的特定需求,同时保持高效性和可维护性。


一、为什么要封装?

提升编程效率:封装unordered_set和map可以简化代码,减少重复劳动。通过创建通用的、可重用的封装,你可以在多个项目中快速部署这些数据结构,而无需每次都从头开始实现。
增强代码可读性封装后的容器往往具有更清晰的接口和更直观的命名,这使得代码更易于理解和维护。对于团队项目来说,这尤其重要,因为它可以降低新成员理解代码的难度。
优化性能:在某些情况下,直接使用标准库的容器可能不是最优解。通过封装,你可以根据具体需求定制容器的行为,比如调整哈希函数、比较器或添加缓存机制,从而提升性能。
扩展功能:标准库的容器提供了基础功能,但可能无法满足所有需求。封装允许你添加额外的功能,如持久化、线程安全、事件监听等,使容器更加适应复杂的应用场景。

二、unordered_set的封装

1.模板参数列表的改造

代码如下(示例):

// K:关键码类型
// V: 不同容器V的类型不同,如果是unordered_map,V代表一个键值对,如果是
unordered_set,V 为 K
// KeyOfValue: 因为V的类型不同,通过value取key的方式就不同,详细见
unordered_map/set的实现
// HF: 哈希函数仿函数对象类型,哈希函数使用除留余数法,需要将Key转换为整形数字才能
取模
template<class K, class V, class KeyOfValue, class Hash = HashFunc<K> >
class HashBucket;
template<class K, class Hash = HashFunc<K>>
class unordered_set
{
public:
	struct SetKeyOfT
	{
		const K& operator()(const K& key)
		{
			return key;
		}
	};
private:
	HashBucket::HashTable<K, K, SetKeyOfT, Hash> _ht;
};

SetKeyOfT是用来获取k的key,用于比较; 

2. 增加迭代器操作

代码如下(示例):

// 前置声明
template<class K, class T, class KeyOfT, class Hash>
class HashTable;

template<class K, class T, class Ref, class Ptr, class KeyOfT, class Hash>
struct __HashIterator
{
	typedef HashNode<T> Node;
	typedef HashTable<K, T, KeyOfT, Hash> HT;
	typedef __HashIterator<K, T, Ref, Ptr, KeyOfT, Hash> Self;

	typedef __HashIterator<K, T, T&, T*, KeyOfT, Hash> Iterator;

	Node* _node;
	const HT* _ht;

	__HashIterator(Node* node, const HT* ht)
		:_node(node)
		, _ht(ht)
	{}

	__HashIterator(const Iterator& it)
		:_node(it._node)
		, _ht(it._ht)
	{}

	Ref operator*()
	{
		return _node->_data;
	}

	Ptr operator->()
	{
		return &_node->_data;
	}

	bool operator!=(const Self& s)
	{
		return _node != s._node;
	}

	// ++it 17:05继续
	Self& operator++()
	{
		if (_node->_next != nullptr)
		{
			_node = _node->_next;
		}
		else
		{
			// 找下一个不为空的桶
			KeyOfT kot;
			Hash hash;
			// 算出我当前的桶位置
			size_t hashi = hash(kot(_node->_data)) % _ht->_tables.size();
			++hashi;
			while (hashi < _ht->_tables.size())
			{
				if (_ht->_tables[hashi])
				{
					_node = _ht->_tables[hashi];
					break;
				}
				else
				{
					++hashi;
				}
			}

			// 没有找到不为空的桶
			if (hashi == _ht->_tables.size())
			{
				_node = nullptr;
			}
		}

		return *this;
	}
};

3. 模板参数的意义

我们的Hash模板参数是用来获取key的类型将其转换为整型,方便与在顺序表里找桶的位置;因为string类型的不像整型一样可以直接找到顺序表里放桶的位置;

KeyOfT用来将指针所指向的数据转换为key类型。

三、unordered_map的封装 

1、“轮子所需要的参数

template<class K, class V, class Hash = HashFunc<K>>
class unordered_map
{
public:
	struct MapKeyOfT
	{
		const K& operator()(const pair<K, V>& kv)
		{
			return kv.first;
		}
	};
public:
	typedef typename HashBucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash>::iterator iterator;

	typedef typename HashBucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash>::const_iterator const_iterator;

	

private:
	HashBucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash> _ht;
};

 因为我们在封装时已经将大部分的模板参数给封装好了,只需要改变MapKeyOfT的()重载即可;

2、迭代器

可以直接复用set封装时实现的;

四、完整代码

1、HashTable

template<>
struct HashFunc<string>
{
	// BKDR
	size_t operator()(const string& s)
	{
		size_t hash = 0;
		for (auto ch : s)
		{
			hash += ch;
			hash *= 31;
		}

		return hash;
	}
};


namespace HashBucket
{
	template<class T>
	struct HashNode
	{
		HashNode<T>* _next;
		T _data;

		HashNode(const T& data)
			:_next(nullptr)
			, _data(data)
		{}
	};

	// 前置声明
	template<class K, class T, class KeyOfT, class Hash>
	class HashTable;

	template<class K, class T, class Ref, class Ptr, class KeyOfT, class Hash>
	struct __HashIterator
	{
		typedef HashNode<T> Node;
		typedef HashTable<K, T, KeyOfT, Hash> HT;
		typedef __HashIterator<K, T, Ref, Ptr, KeyOfT, Hash> Self;

		typedef __HashIterator<K, T, T&, T*, KeyOfT, Hash> Iterator;

		Node* _node;
		const HT* _ht;

		__HashIterator(Node* node, const HT* ht)
			:_node(node)
			, _ht(ht)
		{}

		__HashIterator(const Iterator& it)
			:_node(it._node)
			, _ht(it._ht)
		{}

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}

		bool operator!=(const Self& s)
		{
			return _node != s._node;
		}

		// ++it 17:05继续
		Self& operator++()
		{
			if (_node->_next != nullptr)
			{
				_node = _node->_next;
			}
			else
			{
				// 找下一个不为空的桶
				KeyOfT kot;
				Hash hash;
				// 算出我当前的桶位置
				size_t hashi = hash(kot(_node->_data)) % _ht->_tables.size();
				++hashi;
				while (hashi < _ht->_tables.size())
				{
					if (_ht->_tables[hashi])
					{
						_node = _ht->_tables[hashi];
						break;
					}
					else
					{
						++hashi;
					}
				}

				// 没有找到不为空的桶
				if (hashi == _ht->_tables.size())
				{
					_node = nullptr;
				}
			}

			return *this;
		}
	};

	template<class K, class T, class KeyOfT, class Hash>
	class HashTable
	{
		template<class K, class T, class Ref, class Ptr, class KeyOfT, class Hash>
		friend struct __HashIterator;

		typedef HashNode<T> Node;
	public:
		typedef __HashIterator<K, T, T&, T*, KeyOfT, Hash> iterator;
		typedef __HashIterator<K, T, const T&, const T*, KeyOfT, Hash> const_iterator;

		iterator begin()
		{
			Node* cur = nullptr;
			for (size_t i = 0; i < _tables.size(); ++i)
			{
				cur = _tables[i];
				if (cur)
				{
					break;
				}
			}

			return iterator(cur, this);
		}

		iterator end()
		{
			return iterator(nullptr, this);
		}

		const_iterator begin() const
		{
			Node* cur = nullptr;
			for (size_t i = 0; i < _tables.size(); ++i)
			{
				cur = _tables[i];
				if (cur)
				{
					break;
				}
			}

			return const_iterator(cur, this);
		}

		const_iterator end() const
		{
			return const_iterator(nullptr, this);
		}

		~HashTable()
		{
			for (auto& cur : _tables)
			{
				while (cur)
				{
					Node* next = cur->_next;
					delete cur;
					cur = next;
				}

				cur = nullptr;
			}
		}

		iterator Find(const K& key)
		{
			if (_tables.size() == 0)
				return end();

			KeyOfT kot;
			Hash hash;
			size_t hashi = hash(key) % _tables.size();
			Node* cur = _tables[hashi];
			while (cur)
			{
				if (kot(cur->_data) == key)
				{
					return iterator(cur, this);
				}

				cur = cur->_next;
			}

			return end();
		}

		bool Erase(const K& key)
		{
			Hash hash;
			KeyOfT kot;
			size_t hashi = hash(key) % _tables.size();
			Node* prev = nullptr;
			Node* cur = _tables[hashi];
			while (cur)
			{
				if (kot(cur->_data) == key)
				{
					if (prev == nullptr)
					{
						_tables[hashi] = cur->_next;
					}
					else
					{
						prev->_next = cur->_next;
					}
					delete cur;

					return true;
				}
				else
				{
					prev = cur;
					cur = cur->_next;
				}
			}

			return false;
		}

		// size_t newsize = GetNextPrime(_tables.size());
		size_t GetNextPrime(size_t prime)
		{
			// SGI
			static const int __stl_num_primes = 28;
			static const unsigned long __stl_prime_list[__stl_num_primes] =
			{
				53, 97, 193, 389, 769,
				1543, 3079, 6151, 12289, 24593,
				49157, 98317, 196613, 393241, 786433,
				1572869, 3145739, 6291469, 12582917, 25165843,
				50331653, 100663319, 201326611, 402653189, 805306457,
				1610612741, 3221225473, 4294967291
			};

			size_t i = 0;
			for (; i < __stl_num_primes; ++i)
			{
				if (__stl_prime_list[i] > prime)
					return __stl_prime_list[i];
			}

			return __stl_prime_list[i];
		}

		pair<iterator, bool> Insert(const T& data)
		{
			KeyOfT kot;
			iterator it = Find(kot(data));
			if (it != end())
			{
				return make_pair(it, false);
			}

			Hash hash;

			// 负载因因子==1时扩容
			if (_n == _tables.size())
			{
				/*size_t newsize = _tables.size() == 0 ? 10 : _tables.size()*2;
				HashTable<K, V> newht;
				newht.resize(newsize);
				for (auto cur : _tables)
				{
					while (cur)
					{
						newht.Insert(cur->_kv);
						cur = cur->_next;
					}
				}

				_tables.swap(newht._tables);*/

				//size_t newsize = _tables.size() == 0 ? 10 : _tables.size() * 2;
				size_t newsize = GetNextPrime(_tables.size());
				vector<Node*> newtables(newsize, nullptr);
				//for (Node*& cur : _tables)
				for (auto& cur : _tables)
				{
					while (cur)
					{
						Node* next = cur->_next;

						size_t hashi = hash(kot(cur->_data)) % newtables.size();

						// 头插到新表
						cur->_next = newtables[hashi];
						newtables[hashi] = cur;

						cur = next;
					}
				}

				_tables.swap(newtables);
			}

			size_t hashi = hash(kot(data)) % _tables.size();
			// 头插
			Node* newnode = new Node(data);
			newnode->_next = _tables[hashi];
			_tables[hashi] = newnode;

			++_n;
			return make_pair(iterator(newnode, this), false);;
		}

		size_t MaxBucketSize()
		{
			size_t max = 0;
			for (size_t i = 0; i < _tables.size(); ++i)
			{
				auto cur = _tables[i];
				size_t size = 0;
				while (cur)
				{
					++size;
					cur = cur->_next;
				}

				//printf("[%d]->%d\n", i, size);
				if (size > max)
				{
					max = size;
				}
			}

			return max;
		}
	private:
		vector<Node*> _tables; // 指针数组
		size_t _n = 0; // 存储有效数据个数
	};
}

2、unordered_set

#pragma once
#include"HashTable.h"



namespace my_unordered_set
{
	template<class K, class Hash = HashFunc<K>>
	class unordered_set
	{
	public:
		struct SetKeyOfT
		{
			const K& operator()(const K& key)
			{
				return key;
			}
		};
	public:
		typedef typename HashBucket::HashTable<K, K, SetKeyOfT, Hash>::const_iterator iterator;
		typedef typename HashBucket::HashTable<K, K, SetKeyOfT, Hash>::const_iterator const_iterator;


		iterator begin()
		{
			return _ht.begin();
		}

		iterator end()
		{
			return _ht.end();
		}

		const_iterator begin() const
		{
			return _ht.begin();
		}

		const_iterator end() const
		{
			return _ht.end();
		}

		pair<iterator, bool> insert(const K& key)
		{
			return _ht.Insert(key);
		}

		iterator find(const K& key)
		{
			return _ht.Find(key);
		}

		bool erase(const K& key)
		{
			return _ht.Erase(key);
		}

	private:
		HashBucket::HashTable<K, K, SetKeyOfT, Hash> _ht;
	};

	void print(const unordered_set<int>& s)
	{
		unordered_set<int>::const_iterator it = s.begin();
		while (it != s.end())
		{
			//*it = 1;
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	void test_unordered_set1()
	{
		int a[] = { 3, 33, 2, 13, 5, 12, 1002 };
		unordered_set<int> s;
		for (auto e : a)
		{
			s.insert(e);
		}

		s.insert(54);
		s.insert(107);


		unordered_set<int>::iterator it = s.begin();
		while (it != s.end())
		{
			//*it = 1;
			cout << *it << " ";
			++it;
		}
		cout << endl;

		for (auto e : s)
		{
			cout << e << " ";
		}
		cout << endl;

		print(s);
	}

}

 3、unordered_map

#pragma once

#include "HashTable.h"

namespace my_unordered_map
{
	template<class K, class V, class Hash = HashFunc<K>>
	class unordered_map
	{
	public:
		struct MapKeyOfT
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename HashBucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash>::iterator iterator;

		typedef typename HashBucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash>::const_iterator const_iterator;

		iterator begin()
		{
			return _ht.begin();
		}

		iterator end()
		{
			return _ht.end();
		}

		const_iterator begin() const
		{
			return _ht.begin();
		}

		const_iterator end() const
		{
			return _ht.end();
		}

		pair<iterator, bool> insert(const pair<K, V>& kv)
		{
			return _ht.Insert(kv);
		}

		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = insert(make_pair(key, V()));
			return ret.first->second;
		}

		iterator find(const K& key)
		{
			return _ht.Find(key);
		}

		bool erase(const K& key)
		{
			return _ht.Erase(key);
		}

	private:
		HashBucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash> _ht;
	};


}

总结

封装在于对模板和迭代器的底层逻辑和重写

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2138856.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

前端实战:使用JS和Canvas实现运算图形验证码(uniapp、微信小程序同样可用)

图形验证码是网站安全防护的重要组成部分&#xff0c;能有效防止自动化脚本进行恶意操作&#xff0c;如何实现一个简单的运算图形验证码&#xff1f;本文封装了一个简单的js类&#xff0c;可以用于生成简单但安全的图形验证码。它支持自定义验证码样式&#xff0c;包括字体大小…

驾校预约学习系统的设计与实现

摘 要 伴随着信息技术与互联网技术的不断发展&#xff0c;人们进到了一个新的信息化时代&#xff0c;传统管理技术性没法高效率、容易地管理信息内容。为了实现时代的发展必须&#xff0c;提升管理高效率&#xff0c;各种各样管理管理体系应时而生&#xff0c;各个领域陆续进到…

计算机毕业设计 扶贫助农系统的设计与实现 Java实战项目 附源码+文档+视频讲解

博主介绍&#xff1a;✌从事软件开发10年之余&#xff0c;专注于Java技术领域、Python人工智能及数据挖掘、小程序项目开发和Android项目开发等。CSDN、掘金、华为云、InfoQ、阿里云等平台优质作者✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精…

C++进阶 二叉搜索树的讲解

二叉搜索树的概念 二叉搜索树又称为二叉排序树。 二叉搜索树的性质 若它的左子树不为空&#xff0c;则左子树上所有结点的值都小于等于根结点的值若它的右子树不为空&#xff0c;则右子树上所有结点的值都大于等于根结点的值它的左右子树也分别为二叉搜索树二叉搜索树中可以支持…

【Python】基本使用

目录 变量的类型 整数 int 浮点数 float 字符串 str 字符串长度 格式化字符串 布尔类型 动态类型 注释 获取输入 浮点数比较 多元赋值 for循环 函数的定义和调用 创建函数/定义函数 调用函数/使用函数 列表 创建列表 切片操作 遍历列表 新增元素 判断元…

2024上半年国产操作系统卖疯了!麒麟4.9亿,统信1.9亿!

昨天一篇关于国产数据库上市公司的财报分析&#xff0c;没想到还小&#x1f525;了一把。实际上国产数据库与操作系统是密不可分的&#xff0c;既然用户要进行全面国产化&#xff0c;那么除了数据库&#xff0c;底层服务器操作系统&#xff0c; 一定也会是国产操作系统。 从20…

JavaScript事件处理和常用对象

文章目录 前言一、事件处理程序 1.JavaScript 常用事件2.事件处理程序的调用二、常用对象 1.Window 对象2.String 对象3.Date 对象总结 前言 JavaScript 语言是事件驱动型的。这意味着&#xff0c;该门语言可以通过事件触发来调用某一函数或者一段代码。该文还简单介绍了Window…

讨论人机交互研究中大语言模型的整合与伦理问题

概述 论文地址&#xff1a;https://arxiv.org/pdf/2403.19876.pdf 近年来&#xff0c;大规模语言模型发展迅速。它们给研究和教育领域带来了许多变化。这些模型也是对人机交互&#xff08;HCI&#xff09;研究过程的有力补充&#xff0c;可以分析定性和定量数据&#xff0c;再…

4.C_数据结构_队列

概述 什么是队列&#xff1a; 队列是限定在两端进行插入操作和删除操作的线性表。具有先入先出(FIFO)的特点 相关名词&#xff1a; 队尾&#xff1a;写入数据的一段队头&#xff1a;读取数据的一段空队&#xff1a;队列中没有数据&#xff0c;队头指针 队尾指针满队&#…

springboot 整合quartz定时任务

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、pom的配置1.加注解 二、使用方法1.工程图2.创建工具类 三、controller 实现 前言 提示&#xff1a;这里可以添加本文要记录的大概内容&#xff1a; 提示&a…

微服务、云计算、分布式开发全套课程课件,来原于企培和多年大厂工作提炼

本课内容为笔者16年企业工作期间企培经验总结的 全套课件。需要自取&#xff0c;已分块和整体上传至资源下载中。 全部来源于笔者多年企业培训迭代整理&#xff0c;并做了特殊处理&#xff0c;所以内容无涉密和版权麻烦。 课件内容全部来源于笔者在京东、58、阿里&#xff1b;中…

第二百三十二节 JPA教程 - JPA教程 - JPA ID自动生成器示例、JPA ID生成策略示例

JPA教程 - JPA ID自动生成器示例 我们可以将id字段标记为自动生成的主键列。 数据库将在插入时自动为id字段生成一个值数据到表。 例子 下面的代码来自Person.java。 package cn.w3cschool.common;import javax.persistence.Entity; import javax.persistence.GeneratedValu…

java内存概述

运行时数据区域 Java 虚拟机在执行 Java 程序的过程中会把它所管理的内存划分为若干个不同的数据区域。这些区域有各自的用途&#xff0c;以及创建和销毁的时间&#xff0c;有的区域随着虚拟机进程的启 动而一直存在&#xff0c;有些区域则是依赖用户线程的启动和结束而建立和…

charls基于夜神模拟器抓取安卓7.0应用程序https请求

charls基于夜神模拟器抓取安卓7.0应用程序https请求 1、安装charls&#xff08;安装步骤这里就不详细说了&#xff09;2、下载证书&#xff08;证书后缀名 xx.pem&#xff09;3、使用git bash生成证书hash4、上传证书到安卓的系统证书目录下&#xff08;夜神模拟器方案&#xf…

C++速通LeetCode简单第9题-二叉树的最大深度

深度优先算法递归&#xff1a; /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right…

屏幕缺陷检测-目标检测数据集(包括VOC格式、YOLO格式)

屏幕缺陷检测-目标检测数据集&#xff08;包括VOC格式、YOLO格式&#xff09; 数据集&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1mb83CzAAOkvMZ_LS9Alt8w?pwdagi6 提取码&#xff1a;agi6 数据集信息介绍&#xff1a; 共有 3789 张图像和一一对应的标注文件 标…

RPC远程调用的序列化框架

序列化框架对比&#xff1a; 一、Java Serialiazer 字段serialVersionUID的作用是为了在序列化时保持版本的兼容性&#xff0c;即版本升级时反序列化仍保持对象的唯一性。 //序列化 ByteArrayOutputStream bout new ByteArrayOutputStream(); ObjectOutoutStream out new O…

【Kubernetes笔记】为什么DNS解析会超时?

【Kubernetes笔记】为什么DNS解析会超时&#xff1f; 目录 1 问题背景2 产生后续的问题3 DNS 负缓存工作原理&#xff1a;4 如何解决和缓解 DNS 负缓存 4.1 减小负缓存 TTL4.2 重试机制4.3 减少 Pod 的频繁重启或调度4.4 使用 Headless Service4.5 手动刷新 DNS 缓存 5 总结 …

苹果cms多语言插件,插件配置前端默认语言采集语言等

苹果CMS&#xff08;maccmscn&#xff09;是一款功能强大的内容管理系统&#xff0c;广泛应用于视频网站和其他内容发布平台。为了满足全球用户的需求&#xff0c;苹果CMS支持多语言插件&#xff0c;使得网站能够方便地提供多语言版本。以下是关于苹果CMS多语言插件的详细介绍&…

网络原理2-网络层与数据链路层

目录 网络层数据链路层 网络层 网络层做的工作&#xff1a; 1、地址管理–>IP地址 2、路由选择–>数据包传输的路径规划 网络层主要的协议就是IP协议 IP协议的报头结构&#xff1a; 4位版本&#xff1a; 有两个取值&#xff0c;4表示IPv4&#xff0c;6表示IPv6&am…