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, ...
随机推荐
- 开源项目02-OSharp
项目名称:OSharp 项目所用技术栈: osharp netstandard aspnetcore osharpns ng-alain angular等 项目简介: OSharp是一个基于.NetC ...
- 微信对话平台API开发
接入官方文档接入文档 下面我们开始使用前端来进行接入 <!DOCTYPE html> <html> <head> <meta charset="UT ...
- 开源即时通讯(IM)项目OpenIM源码部署流程
由于 OpenIM 依赖的组件较多,开发者需求不一,导致 OpenIM 部署一直被人诟病,经过几次迭代优化,包括依赖的组件 compose 的一键部署,环境变量设置一次,全局生效,以及脚本重构,目前 ...
- XXE注入详解
XML介绍 XML全称可扩展标记语言(EXtensible Markup Language),XML跟HTML格式类似,但是作用不同,XML侧重于数据传输,HTML注重于标记语言,也就是说XML其实是 ...
- CF1706E Qpwoeirut and Vertices 题解
题目链接:CF 或者 洛谷 官解看上去挺智慧的,来点朴素的解法.我们来当做纯 ds 题去做.首先明确一点,图中若干个点关于最早连通性的这个问题可以考虑 \(MST\),我们有一类东西叫 \(krusk ...
- mybatis批量插入支持默认值和自定义id生成策略的免写sql插件
最近做项目时用了免写sql的插件但是发现批量操作不满足现有需求.所以,在原有基础之上扩展了批量的操作支持[支持插入默认值和自定义id生成策略].使用方法如下: 一:在pom文件中引入jar配置 < ...
- 设计模式 - 创建型模式 - 单例模式(C++)
1.前言 单例模式属于创建型模式,保证一个类仅有一个实例,并提供一个访问它的全局访问点. 单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例,这个类称为单例类,它提供全局访问的方 ...
- 鹏程杯子2023 pwn
主要就是修改stdin的最后几位,使他变为write,然后泄露libc,为所欲为即可. 本人是卡在不知道stdin那里可以修改. 然后使用一下jmp qword rbp这个gadget 0x400a9 ...
- DecisionTreeClassifier&DecisionTreeClassRegression
DecisionTreeClassifier from sklearn.datasets import load_wine # 红酒数据集 from sklearn.tree import Decis ...
- JS Leetcode 496. 下一个更大元素 I 更清晰的图解单调栈做法
壹 ❀ 引 最近一周的工作压力很大...一周的时间一直在处理一个APP漏洞问题,因为项目三年无人维护,突然要改东西光是修改构建错误以及三方包依赖错误就花了三天时间= =.不过好在问题到已经结束尾,闲下 ...