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. MySQL查询排序和分页

    连接数据库 mysql -hlocalhost -uroot -proot 排序查询语法: select 字段列表 from 表名 order by 字段1 排序方式1, 字段3 排序方式2,字段3 ...

  2. vue混入mixin

    <div id="app"> --{{nick11}} </div> <script> // 全局混入 不需要注册 var m1 = Vue.m ...

  3. 利用pearcmd.php本地文件包含(LFI)

    本文主要是为了学习如何用pearcmd进行本地文件包含 0x00  环境准备 首先先在docker中安装一个php环境. docker exec -it [container id] /bin/bas ...

  4. C语言输出狗头

    使用printf()函数输出样式 #include <stdio.h> int main() { printf(" * ii. ;9ABH,\n"); printf(& ...

  5. [1] 以逆向的角度来看流程控制语句——if

    [1] 以逆向的角度来看流程控制语句--if 1. if语句(单分支) ​ if语句转换的条件跳转指令与if语句的判断结果是相反的, 因为C语言是根据代码行的位置决定编译后二进制代码地址高低的,即低行 ...

  6. Spring框架源码分析

    目录 Spring核心思想 Spring源码编译 自定义实现Spring框架IOC与DI Spring源码Ioc核心模块分析 BeanDefinition整体介绍 FactoryBean接口的使用 B ...

  7. 5.9 Windows驱动开发:内核InlineHook挂钩技术

    在上一章<内核LDE64引擎计算汇编长度>中,LyShark教大家如何通过LDE64引擎实现计算反汇编指令长度,本章将在此基础之上实现内联函数挂钩,内核中的InlineHook函数挂钩其实 ...

  8. 辣鸡 mac 下 pycharm 中代码拖拽的问题

    Editor –> General Enable Drag'n'Drop functionality in Editor

  9. ECMAScript 2023 新特性预览

    ECMAScript 2023 的最终版本预计将于今年 6 月底发布.会议基本已经确定 了 ECMAScript 2023 的新功能列表,预计不会再有任何重大的编辑更改. 着该提案已被 ECMAScr ...

  10. unordered_map模拟实现|STL源码剖析系列|开散列

    博主很久没有更新过STL源码剖析这个系列的文章了,主要是因为大部分STL常用的容器,博主都已经发过文章了,今天博主带着大家把哈希表也模拟实现一下. 前言 那么这里博主先安利一下一些干货满满的专栏啦! ...