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, ...
随机推荐
- Object.defineProperty熬夜整理的用法,保证你看的明白!
Object.defineProperty的基本使用 <script> let personObj={ name:'何西亚', sex:'男' } //我们想给这个对象添加一个属性 // ...
- 你不知道的Promise构造函数Promise(excutor)
Promise构造函数Promise(excutor) // 说明一下:excutor会在Promise内部立刻同步调用:(异步操作在执行器执行) var p = new Promise((resol ...
- elementUI(datepicker)限制日日期的选择
指定起始日期,后选的将会受到先选的限制 参考地址 https://www.jianshu.com/p/c59c8ef6c500 实现方法不难,利用了 change 事件,动态改变 picker-opt ...
- 如何计算一个uint64类型的二进制值的尾部有多少个0
作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu 公众号:一本正经的瞎扯 正文 这实在是一个很简单的问题,用代码可以表示如下: func Coun ...
- 解决: DECODER_ERROR_CLASSES += (brotli.error,) ttributeError: module ‘brotli‘ has no attribute ‘error‘
解决: DECODER_ERROR_CLASSES += (brotli.error,) ttributeError: module 'brotli' has no attribute 'error' ...
- 多智能体强化学习算法【三】【QMIX、MADDPG、MAPPO】
相关文章: 常见多智能体强化学习仿真环境介绍[一]{推荐收藏,真的牛} 多智能体强化学习算法[一][MAPPO.MADDPG.QMIX] 多智能体强化学习算法[二][MADDPG.QMIX.MAPPO ...
- 使用Miniconda安装R语言环境
使用Miniconda安装R语言 下载Miniconda3-latest-Linux-x86_64.sh 静默安装 sh Miniconda3-latest-Linux-x86_64.sh -b -p ...
- Hive-分区取TOP N问题
问题背景 设想你对用户在不同品类上的行为打分聚合后得到这样一个表 user_cate_score uid cate score 1 1 0.3 2 2 0.5 8 3 0.9 现在,你想将每个品类的T ...
- NC16591 [NOIP2010]关押罪犯
题目链接 题目 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气 ...
- 基于 log4j2 插件实现统一日志脱敏,性能远超正则替换
前言 金融用户敏感数据如何优雅地实现脱敏? 日志脱敏之后,无法根据信息快速定位怎么办? 经过了这两篇文章之后,我们对日志脱敏应该有了一定的理解. 但是实际项目中,我们遇到的情况往往更加复杂: 1)项目 ...