当前位置: 首页 > news >正文

【C++之unordered_set和unordered_map的模拟实现】

C++学习笔记---025

  • C++之unordered_set和unordered_map的模拟实现
    • 1、unordered_set的模拟实现
    • 2、unordered_map的模拟实现

C++之unordered_set和unordered_map的模拟实现

前言:
前面篇章学习了C++对unordered_set和unordered_map的认识和应用,接下来继续学习,C++的unordered_set和unordered_map模拟实现等知识。
/知识点汇总/

1、unordered_set的模拟实现

#define _CRT_SECURE_NO_WARNINGS 1#include "HashBucket.h"namespace bit1
{template<class K, class Hash = HashFunc<K>>//class Hash = HashFunc<K>修改参数在这里传入class unordered_set{struct SetKeyOfT{const K& operator()(const K& key){return key;}};public://迭代器typedef typename HashTable<K,const K, SetKeyOfT, Hash>::Iterator iterator;typedef typename HashTable<K, const 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();//这里报错,是因为End()中的返回值,this指针问题}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://HashTable<K, V> _ht;HashTable<K,const K, SetKeyOfT, Hash> _ht;//+const};void test_unordered_set(){unordered_set<int> s;s.insert(31);s.insert(11);s.insert(5);s.insert(15);s.insert(25);unordered_set<int>::iterator it = s.begin();while (it != s.end()){//*it += 1;//set是不能被修改的,需要const修饰cout << *it << " ";++it;}cout << endl;for (auto e : s){cout << e << " ";}cout << endl;}//const迭代器void Func(const unordered_set<int>& s){unordered_set<int>::iterator it = s.begin();while (it != s.end()){//*it = 1;cout << *it << " ";++it;}cout << endl;}
}

2、unordered_map的模拟实现

#define _CRT_SECURE_NO_WARNINGS 1#include "HashBucket.h"namespace bit1
{template<class K, class V,class Hash = HashFunc<K>>class unordered_map{struct MapKeyOfT{const K& operator()(const pair<K, V>& kv){return kv.first;}};public:typedef typename HashTable<K, pair<const K, V>, MapKeyOfT, Hash>::Iterator iterator;iterator begin(){return _ht.Begin();}iterator end(){return _ht.End();}V& operator[](const K& key){pair<iterator, bool> ret = insert(make_pair(key, V()));return ret.first->second;//报错发现->还没有写}// 21:15pair<iterator, bool> insert(const pair<K, V>& kv){return _ht.Insert(kv);}private:HashTable<K, pair<const K, V>, MapKeyOfT, Hash> _ht;};void test_unordered_map(){string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜","苹果", "香蕉", "苹果", "香蕉","苹果","草莓", "苹果","草莓" };unordered_map<string, int> countMap;for (auto& e : arr){countMap[e]++;}unordered_map<string, int>::iterator it = countMap.begin();while (it != countMap.end()){//it->first += 'x'; // key不能修改it->second += 1;  // value可以修改cout << it->first << ":" << it->second << endl;++it;}cout << endl;for (auto& kv : countMap){cout << kv.first << ":" << kv.second << endl;}cout << endl;}
}
http://www.lryc.cn/news/388804.html

相关文章:

  • 服务器使用别人的conda
  • 农村程序员陈随易2024年中总结
  • Spring Boot中的日志管理最佳实践
  • python基础语法 004-2流程控制- for遍历
  • 【高考志愿】医学
  • 音视频开发31 FFmpeg 编码- avcodec_find_encoder和avcodec_find_encoder_by_name
  • 大模型压缩:基于贝叶斯优化的自适应低秩分解
  • 【Python函数编程实战】:从基础到进阶,打造代码复用利器
  • ZooKeeper 应用场景深度解析
  • 动手学深度学习(Pytorch版)代码实践 -计算机视觉-41目标检测数据集
  • 2.2章节python的变量和常量
  • 豆包文科成绩超了一本线,为什么理科不行?
  • Java多线程编程实践中的常见问题与解决方案
  • WebStorm配置路径别名(jsconfig.json)
  • [吃瓜教程]南瓜书第4章决策树
  • Redis 面试题完整指南:深度解析基础、进阶与高级功能
  • spring 枚举、策略模式、InitializingBean初使化组合使用示例
  • 嵌入式学习——硬件(IIC、ADC)——day56
  • vCenter VXR01405C ALARM Certificate is about to expire
  • 安装和微调大模型(基于LLaMA-Factory)
  • 使用docker搭建squid和ss5
  • 大数据面试题之Flink(1)
  • 策略模式、工厂模式和模板模式的应用
  • 在postman中调试supabase的API接口
  • 微信小程序毕业设计-英语互助系统项目开发实战(附源码+论文)
  • 【WEB前端2024】3D智体编程:乔布斯3D纪念馆-第49课-机器人自动跳舞
  • 【LLM教程-llama】如何Fine Tuning大语言模型?
  • PHP 比 Java 的开发效率高在哪?
  • 高德定位获取详细位置失败的处理方法
  • PX2平台Pytorch源码编译