STL-unordered_map,unordered_set模拟实现
unordered_set
#pragma once #include"28hashtable_container.h" namespace test
{
//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; template<class K, class Hash = HashBucket::HashFunc<K>>
class unordered_set
{
private:
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; public:
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; }; //unordered_set_end; void test_My_unordered_set1()
{
int a[] = { 3, 33, 2, 13, 5, 12, 1002 }; test::unordered_set<int> s;
//s.insert(1); for (auto i : a)
{
s.insert(i);
}
s.insert(54);
s.insert(107); s.erase(33); test::unordered_set<int>::iterator it = s.begin();
while (it!=s.end())
{
cout << *it << endl;
++it;
} } }
unordered_map
#pragma once #include"28hashtable_container.h"
#include"1Date.h"//测试用 namespace test
{
//template < class Key, // unordered_map::key_type
// class T, // unordered_map::mapped_type
// class Hash = hash<Key>, // unordered_map::hasher //用于支持取模的仿函数
// class Pred = equal_to<Key>, // unordered_map::key_equal //用于支持==重载的仿函数
// class Alloc = allocator< pair<const Key, T> > // unordered_map::allocator_type
//> class unordered_map; template<class K,class V,class Hash = HashBucket::HashFunc<K>>
class unordered_map
{ private:
struct mapKeyOfT
{
const K& operator()(const pair<const 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; public: 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; //模拟的是指针,ret.first是iterator,iterator->可以直接访问到node的成员
} private:
HashBucket::HashTable<K, pair<const K, V>, mapKeyOfT, Hash> _ht; }; //unordered_map_end void test_My_unordered_map1()
{
unordered_map<int, int> m;
m.insert(make_pair(1, 1));
m.insert(make_pair(3, 3));
m.insert(make_pair(2, 2)); //unordered_map<int, int>::const_iterator it = m.begin(); //测试没问题
unordered_map<int, int>::iterator it = m.begin();
while (it != m.end())
{
//it->first = 1;
//it->second = 1; cout << it->first << ":" << ++it->second << endl;
++it;
}
cout << endl;
} void test_My_unordered_map2()
{
std::string arr[] = { "西瓜", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉", "梨" };
unordered_map<std::string, int> countMap;
for (auto& e : arr)
{
countMap[e]++;
} for (auto& kv : countMap)
{
cout << kv.first << ":" << kv.second << endl;
}
} struct HashDate
{
size_t operator()(const Date& d)
{
size_t ret = 0;
ret += d._year;
ret *= 31;
ret += d._month;
ret *= 31;
ret += d._day;
ret *= 31;
return ret;
}
}; void test_My_unordered_map3()
{ // 自定义类型作map,set的key需要支持比较大小(需要重载<) ,只需要重载一个 '<' 或 '>' 就可以比较大小 ,
//自定义类型作unordered的key需要满足1.要有可以取模的对象 2.支持比较是否相等,hashtable需要比较key(需要重载==)
/**
* stl::如果作为哈希key的自定义类型不支持等于,stl的哈希map支持传仿函数 class Pred = equal_to<Key>用于自己适配
*
*/ Date d1(2023, 3, 13);
Date d2(2023, 3, 13);
Date d3(2023, 3, 12);
Date d4(2023, 3, 11);
Date d5(2023, 3, 12);
Date d6(2023, 3, 13); Date a[] = { d1,d2,d3,d4,d5 ,d6}; unordered_map<Date, int, HashDate> m; for (auto& e : a)
{
++m[e];
} for (auto& kv : m)
{
cout << kv.first << ":"<<kv.second << endl;
} } }
STL-unordered_map,unordered_set模拟实现的更多相关文章
- c++ 标准库的各种容器(vector,deque,map,set,unordered_map,unordered_set,list)的性能考虑
转自:http://blog.csdn.net/truexf/article/details/17303263 一.vector vector采用一段连续的内存来存储其元素,向vector添加元素的时 ...
- 【STL】【模拟】Codeforces 696A Lorenzo Von Matterhorn
题目链接: http://codeforces.com/problemset/problem/696/A 题目大意: 一个满二叉树,深度无限,节点顺序编号,k的儿子是k+k和k+k+1,一开始树上的边 ...
- STL: unordered_map 自定义键值使用
使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...
- unordered_map/unordered_set & unordered_multimap/unordered_multiset非关联容器
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- STL::unordered_map/unordered_multimap
unordered_map: 和 unorder_set 相似,该容器内部同样根据 hash value 把键值对存放到相应的 bucket(slot)中,根据单个 key 来访问 value 的速度 ...
- C++ unordered_map/unordered_set 自定义键类型
1. unordered_map 和 unordered_set template < class Key, // unordered_map::key_type class T, // uno ...
- 利用C++ STL的vector模拟邻接表的代码
关于vector的介绍请看 https://www.cnblogs.com/zsq1993/p/5929806.html https://zh.cppreference.com/w/cpp/conta ...
- c++ STL之unordered_set
unordered_set的特点: 自身值就是主键,所以值唯一并且不可修改 基于hash表的无序排列 unordered_set基于哈希表,是无序的. 在一个 unordered_set 容器中,元素 ...
- C++ STL unordered_map
容器unordered_map<key type,value tyep>m; 迭代器unordered_map<key type,value tyep>::iterator i ...
- STL——map/unordered_map基础用法
map /multimap map是STL里重要容器之一. 它的特性总结来讲就是:所有元素都会根据元素的键值key自动排序(也可根据自定义的仿函数进行自定义排序),其中的每个元素都是<key, ...
随机推荐
- 分布式事务和Spanner分布式数据库
一.分布式事务 首先事务可以这么理解:程序员有一些不同的操作,或许针对数据库不同的记录,他们希望所有这些操作作为一个整体,不会因为失败而被分割,也不会被其他活动看到中间状态.事务处理系统要求程序员对这 ...
- TS声明promise返回来的数据类型
promise返回来的数据类型 interface backResult{ code: number, data: { name:string,age:number}[], //数组里面的对象类型,这 ...
- Liunx网络配置
1.安装精简版:CentOS-7-x86_64-Minimal-2009.iso 2.进入配置文件: vi /etc/sysconfig/network-scripts/ifcfg-ens33 3. ...
- 自动化部署实例(donetcore GitLab CICD )
主要简单的介绍了一下 GitLab CI 的持续集成以及持续部署,这篇将通过 GitLab CI 发布一个 .net core 项目,来带小伙伴们感受一下自动化的魅力,从此告别手动发布. 准备工作 创 ...
- 手撕Vue-实现将数据代理到Vue实例
前言 经过上一篇文章的学习,完成了 v-on 指令的实现,接下来我们来实现将数据代理到 Vue 实例上. 为什么要完成这个功能呢?因为我们在使用 Vue 的时候,可以直接通过 this.xxx 的方式 ...
- C++ 实现的Ping类的封装
Ping 使用 Internet 控制消息协议(ICMP)来测试主机之间的连接.当用户发送一个 ping 请求时,则对应的发送一个 ICMP Echo 请求消息到目标主机,并等待目标主机回复一个 IC ...
- 从嘉手札<2023-12-09>
大雪时节 有种风雪欲来的静谧 如同飘摇的浮舟 人们常说上岸 可对于常年生活在水里的鱼儿来说 哪里是岸边呢 我不知道未来 但唯一可以确定的是 无论你过的怎么样 你都需要给自己一个交待 哪怕风雪兼程 哪怕 ...
- 监控Celery不一定非要使用Flower
运维平台中有许多的周期/定时/异步任务,例如证书扫描.数据备份.日志清理.线上作业等等,这些任务的执行都是借助于Celery来完成的.任务多了之后就会遇到一系列的问题,例如我之前写过的将任务分多队列来 ...
- Netty-核心模块组件-4
Netty 核心模块组件 一.Bootstrap.ServerBootstrap 1.Bootstrap 意思是引导,一个 Netty 应用通常由一个 Bootstrap 开始,主要作用是配置整个 N ...
- 如何控制Tomcat的catalina.out的大小
catalina.out文件,数据主要来源为:System.out 和 System.err 在控制台上直接输出的信息. 编码时应避免使用System.out.println()和e.printSta ...