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模拟实现的更多相关文章

  1. c++ 标准库的各种容器(vector,deque,map,set,unordered_map,unordered_set,list)的性能考虑

    转自:http://blog.csdn.net/truexf/article/details/17303263 一.vector vector采用一段连续的内存来存储其元素,向vector添加元素的时 ...

  2. 【STL】【模拟】Codeforces 696A Lorenzo Von Matterhorn

    题目链接: http://codeforces.com/problemset/problem/696/A 题目大意: 一个满二叉树,深度无限,节点顺序编号,k的儿子是k+k和k+k+1,一开始树上的边 ...

  3. STL: unordered_map 自定义键值使用

    使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...

  4. unordered_map/unordered_set & unordered_multimap/unordered_multiset非关联容器

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  5. STL::unordered_map/unordered_multimap

    unordered_map: 和 unorder_set 相似,该容器内部同样根据 hash value 把键值对存放到相应的 bucket(slot)中,根据单个 key 来访问 value 的速度 ...

  6. C++ unordered_map/unordered_set 自定义键类型

    1. unordered_map 和 unordered_set template < class Key, // unordered_map::key_type class T, // uno ...

  7. 利用C++ STL的vector模拟邻接表的代码

    关于vector的介绍请看 https://www.cnblogs.com/zsq1993/p/5929806.html https://zh.cppreference.com/w/cpp/conta ...

  8. c++ STL之unordered_set

    unordered_set的特点: 自身值就是主键,所以值唯一并且不可修改 基于hash表的无序排列 unordered_set基于哈希表,是无序的. 在一个 unordered_set 容器中,元素 ...

  9. C++ STL unordered_map

    容器unordered_map<key type,value tyep>m; 迭代器unordered_map<key type,value tyep>::iterator i ...

  10. STL——map/unordered_map基础用法

    map /multimap map是STL里重要容器之一. 它的特性总结来讲就是:所有元素都会根据元素的键值key自动排序(也可根据自定义的仿函数进行自定义排序),其中的每个元素都是<key,  ...

随机推荐

  1. git查看自己是从那个分支建的分支

    可能发生的情况 很多时候,开始建分支的时候, 能够确认自己是那个分支建的,但是当写完功能之后, 再去回想,有时是忘记自己基于那个分支建的分支. 这时有一个命令的话就可以很快的定位了. 查看创建的分支来 ...

  2. x509: cannot validate certificate for xxx because it doesn't contain any IP SANs

    项目中有时候需要访问https网站,但如果该网站使用的是自建证书,那client端验证server端证书时,有时候会报错: x509: cannot validate certificate for ...

  3. React中引入外部js文件

    假设需要引入本地的一个jquery库: <script src="http://127.0.0.1:8080/plugins/jquery.min.js" type=&quo ...

  4. 有用的sql笔记(工作总结)

    1.查询当前月(数字为0表示当前月份,1表示上个月,-1表示下个月,以此类推) SELECT DATE_FORMAT((CURDATE() - INTERVAL [数字] MONTH), '%Y-%m ...

  5. tp使用workerman消息推送

    安装 首先通过 composer 安装 composer require topthink/think-worker SocketServer 在命令行启动服务端 php think worker:s ...

  6. 如何快速获取AWR中涉及到的表

    最近遇到一个很少见的需求,是关于应用测试方面的. 具体来说,这个应用的测试需求要基于一个固定的时间点数据,而且只能测试一轮,再测试就需要还原到测试前状态. 因为我们使用的存储是分层的(热数据在Flas ...

  7. 程序语言多态(overide) - delphi 版本

    程序语言多态 - delphi 版本 前言: 所有程序语言都差不多,特写一篇 delphi 版本 的多态:其它语言 类同. 都是一些别人规定的语法而已,别人用一个下午设计一门语言,愚弄天下程序员一生: ...

  8. 《ASP.NET Core 与 RESTful API 开发实战》-- (第6章)-- 读书笔记(下)

    第 6 章 高级查询和日志 6.3 排序 RESTful API 在实现排序时应支持对集合资源的一个或多个属性进行排序 示例对 authors 资源按照其属性 Age 升序排序,再按 BirthPla ...

  9. NC14419 线路规划

    题目链接 题目 题目描述 Q国的监察院是一个神秘的组织. 这个组织掌握了整个帝国的地下力量,监察着Q国的每一个人. 监察院一共有N个成员,每一个成员都有且仅有1个直接上司,而他只听从其上直接司的命令. ...

  10. DRF解决跨域问题

    Django Rest Framework提供了corsheaders模块解决跨域问题 安装模块 pip3.9 install django-cors-headers 注册应用 # 注册 corshe ...