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

目录

前言

二、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>
{// BKDRsize_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){// SGIstatic 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.xdnf.cn/news/1536122.html

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

相关文章

2、.Net 前端框架:ASP.Net Core - .Net宣传系列文章

ASP.NET Core 是一个跨平台、高性能、开源的框架&#xff0c;用于构建现代化的、基于云的、互联网连接的应用程序。它是微软对原始ASP.NET框架的重构和扩展&#xff0c;提供了更多的灵活性和改进的性能。ASP.NET Core 可以用于开发Web应用程序、Web API、以及服务端渲染的Web页…

windows系统docker装milvus向量数据库

首先创建一个文件夹比如milvus,在创建如下文件 docker-compose.yml文件如下: version: 3.5services:etcd:container_name: milvus-etcdimage: quay.io/coreos/etcd:v3.5.5environment:- ETCD_AUTO_COMPACTION_MODErevision- ETCD_AUTO_COMPACTION_RETENTION1000- ETCD_QUOTA_B…

计算机毕业设计hadoop+spark+hive物流预测系统 物流大数据分析平台 物流信息爬虫 物流大数据 机器学习 深度学习

流程&#xff1a;1.Python爬虫采集物流数据等存入mysql和.csv文件&#xff1b;2.使用pandasnumpy或者MapReduce对上面的数据集进行数据清洗生成最终上传到hdfs&#xff1b;3.使用hive数据仓库完成建库建表导入.csv数据集&#xff1b;4.使用hive之hive_sql进行离线计算&#xff…

Qt常用控件——QComboBox

文章目录 核心属性、方法、信号模拟点餐文件加载 核心属性、方法、信号 QComboBox表示下拉框 核心属性&#xff1a; 属性说明currentText当前选中文本currentIndex当前选中的条目下标editable是否允许修改设置为true时&#xff0c;QComboBox的行为就非常接近于QLineEdit&…

【智路】智路OS Airos Edge 2.0 Quick Start

Airos Edge 2.0 Quick Start 1 智路OS2.0 1.1 简介 智路OS路侧操作系统airos-edge自下而上分别由内核层&#xff0c;硬件抽象层、框架层、服务层和应用层构成&#xff1b;提供了一系列抽象和框架&#xff0c;支持设备接入、服务、应用等组件开发&#xff0c;兼容X86和ARM操作…

【光照增强论文略读】Zero-Reference Deep Curve Estimation for Low-Light Image Enhancement

这篇题为《用于低光照图像增强的零参考深度曲线估计》的论文介绍了一种名为Zero-DCE的创新方法&#xff0c;用于增强低光照图像。其主要创新点在于&#xff0c;它在训练过程中不需要成对或非成对的参考图像&#xff0c;因此是一种“零参考”方法。通过轻量级深度学习模型DCE-Ne…

SAP学习笔记 - 开发06 - CDSView + Fiori Element 之 List Report

上一章讲了Fiori UI5开发环境搭建和实践&#xff1a; - VSCode 安装Fiori Tools插件 - SEGW 创建后台程序&#xff0c;注册服务&#xff0c;Gateway Client确认服务 - 使用SEGW公开的服务来查询数据显示到页面 SAP学习笔记 - 开发05 - Fiori UI5 开发环境搭建2 Fiori Tools…

北极星计划的回响:从Leap Motion到Midjourney的AI 3D硬件梦想

在科技的浩瀚星空中,总有一些梦想如同北极星般璀璨,指引着探索者前行。六年前,Leap Motion的CEO David以一篇充满激情的博客文章,向我们揭示了“北极星计划”——一个旨在打破数字与物理界限,创造流畅统一体验的增强现实平台。今天,随着Midjourney在AI文生图领域的全球爆…

使用OpenFeign在不同微服务之间传递用户信息时失败

文章目录 起因原因解决方法&#xff1a; 起因 从pay-service中实现下单时&#xff0c;会调用到user-service中的扣减余额。 因此这里需要在不同微服务之间传递用户信息。 但是user-service中始终从始至终拿不到user的信息。 原因 在pay-service中&#xff0c;不仅要Enable O…

Android 10.0 mtk平板camera2横屏预览旋转90度横屏保存圆形预览缩略图旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,点击录像和照片下保存的圆形预览缩略图 依然是竖屏的,所以说同样需要…

【JavaEE】IO基础知识及代码演示

目录 一、File 1.1 观察get系列特点差异 1.2 创建文件 1.3.1 delete()删除文件 1.3.2 deleteOnExit()删除文件 1.4 mkdir 与 mkdirs的区别 1.5 文件重命名 二、文件内容的读写----数据流 1.1 InputStream 1.1.1 使用 read() 读取文件 1.2 OutputStream 1.3 代码演示…

【LLM多模态】文生视频评测基准VBench

note VBench的16个维度自动化评估指标代码实践&#xff08;待完成&#xff09;16个维度的prompt举例人类偏好标注&#xff1a;计算VBench评估结果与人类偏好之间的相关性、用于DPO微调 文章目录 note一、相关背景二、VBench评测基准概述&#xff1a;论文如何解决这个问题&…

yum install时候报错

报错 Another app is currently holding the yum lock; waiting for it to exit 另外一个yum命令完成了死锁大概率是因为执行yum 命令的时候报错&#xff0c;然后强制退出了 解决方法 找到进程杀死进程 ps aux | grep yum这个进程号&#xff1a;你在上述命令和报错中都看的进程…

ubuntu20.04下载cuda11.8

nvidia官方地址&#xff1a;https://developer.nvidia.com/cuda-downloads wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run输入该命令后 sudo sh cuda_11.8.0_520.61.05_linux.run gedit ~/.bashrcexport…

9. Transforms的使用(四)--Compose

Transforms的使用&#xff08;四&#xff09; 1. 为什么要使用Compose类 在深度学习模型的训练过程中&#xff0c;往往需要对图像按顺序进行一系列的变化&#xff0c;如果把系列变化按顺序写成代码会比较冗余Compose实现了将所有的系列变化进行集合的操作&#xff0c;从代码层…

【智路】智路OS air-edge 开发者手册 功能概述

功能概述 https://airos-edge.readthedocs.io/zh/latest/airospkg/airospkg.html 智路OS包支持部署在智路OS开源版本和智路OS发行版。 智路OS发行版&#xff08;airos distribution&#xff09;是基于智路OS的商业化版本。包括智路OS内核层、系统工具、库、软件包管理系统等…

【裸机装机系列】6.kali(ubuntu)-图形界面优化-让linux更适合你的使用习惯

接下来就是图形化界面操作的部分了。会用少量截图来说明&#xff0c;图太多会影响阅读体验&#xff0c;直接文字来描述过程吧。 1> 入口 任务栏左上角——> 开始菜单——> settings——> settings manager 大部分配置都会在这里面设置。 2> 设置里面分的4大…

CS61C 2020计算机组成原理Lab01-数字表示,溢出

1. Exercise 1 :See what you can C # 用gcc 来编译可执行文件如program.c gcc program.c # 就可以得到一个executable file named a.out. ./a.out# 如果想给这个可执行文件命名&#xff0c;则使用 -o gcc -o program program.c ./program# 使用-g 能得到一个 可执行程序的deb…

ElementUI 布局——行与列的灵活运用

ElementUI 布局——行与列的灵活运用 一 . 使用 Layout 组件1.1 注册路由1.2 使用 Layout 组件 二 . 行属性2.1 栅格的间隔2.2 自定义元素标签 三 . 列属性3.1 列的偏移3.2 列的移动 在现代网页设计中&#xff0c;布局是构建用户界面的基石。Element UI 框架通过其强大的 <e…

计算架构模式之接口高可用

接口高可用整体框架 接口高可用主要应对两类问题&#xff1a;雪崩效应和链式效应。 雪崩&#xff1a;当请求量超过系统处理能力之后&#xff0c;会导致系统性能螺旋快速下降&#xff0c;本来系统可以处理1000条&#xff0c;但是当请求量超过1200的时候&#xff0c;此时性能会下…