当前位置: 首页 > news >正文

做电影网站能赚钱吗直播:英格兰vs法国

做电影网站能赚钱吗,直播:英格兰vs法国,武汉襄阳网站推广公司哪家好,专业定制网站系统个人主页:仍有未知等待探索-CSDN博客 专题分栏:C 本文着重于模拟实现哈希表,并非是哈希表的使用。 实现的哈希表的底层用的是线性探测法,并非是哈希桶。 目录 一、标准库中的哈希表 1、unordered_map 2、unordered_set 二、模…

个人主页:仍有未知等待探索-CSDN博客

专题分栏:C++

本文着重于模拟实现哈希表,并非是哈希表的使用。

实现的哈希表的底层用的是线性探测法,并非是哈希桶。

目录

一、标准库中的哈希表

1、unordered_map

2、unordered_set

二、模拟实现哈希表

1、结构

2、注解hashtable的模板参数:

3、重难点

1、二元“!=” 没有找到接收类型的右操作数的运算符​编辑

2、迭代器的本质、const和非const的区别

3、类A在类B的上面,想要在类A中使用以类B为类型的变量,怎么办?

4、类A在类B的上面,想要在类A中使用类B的私有成员,怎么办?

三、代码


一、标准库中的哈希表

这两个是两种不同映射关系的哈希表。

unordered_map:是 Key - Value 的映射关系 (也就是 关键字和键值 之间的映射)

unordered_set:是 Value 的映射关系(也就是只有 键值 的映射)

这两种不同的映射关系从各自的模板参数就能看出来。

1、unordered_map

unordered_map又分为两种,一种是unordered_map,另一种是unordered_multimap。

这两种有什么区别呢?

以unordered_map为例:

这里面的模板参数 Key - T 就是这个容器存储的键值关系。

2、unordered_set

同样:unordered_set又分为两种,一种是unordered_set,另一种是unordered_multiset。

这两个的区别和unordered_map中两个的区别一样。

二、模拟实现哈希表

1、结构

在模拟实现哈希表前,我们要知道整个实现的结构,要有完整性的认识。

这个就是哈希表的大致底层结构。

2、注解hashtable的模板参数:

  • Key:是上层结构(也就是unordered_set、unordered_map)的关键字。
  • T:是存的关键字和键值的映射。

(也就是unordered_set:pair<Key, Key>;unordered_map:pair<Key, Vaule>)

  • Ptr是T*。
  • Ref是T&。
  • KeyofT是一个仿函数,用来取T的Key。因为在这一层也不知道上一层是unordered_set、unordered_map,所以还需要传一个仿函数来进行取值。
  • hashfunc是一个仿函数,用来计算hash映射值。

对于一个整数,怎么存储到哈希表里?需要对其进行hash计算,将这个整数取模于表的大小得到。

对于一个字符串,怎么存储到哈希表里?需要对其进行hash计算,将这个字符串转换成整数,然后将这个整数取模于表的大小得到下标。

如果得到的下标冲突了,就用哈希线性探测法解决冲突。

3、重难点

写完不算难,难的是找bug。

1、二元“!=” 没有找到接收类型的右操作数的运算符

类型不匹配,你可以去你出错的位置看看该类型的模板参数的类型匹不匹配。

2、迭代器的本质、const和非const的区别

迭代器就是模拟的指针的行为。

const迭代器和非const迭代器的区别就是限制其*和->运算符的返回值。

3、类A在类B的上面,想要在类A中使用以类B为类型的变量,怎么办?

在类A的前面加上类B的前置声明即可。

4、类A在类B的上面,想要在类A中使用类B的私有成员,怎么办?

在类B中加上类A的友元。

三、代码

hash.h

#pragma once#include <iostream>
#include <vector>
#include <string>
using namespace std;
enum  State
{Empty,Exist,Delete
};
// hash_func ---> 用来对任意类型进行编码
template<class T>
struct hash_func
{size_t operator()(const T& _val){return (size_t)_val;}
};
// 对string类型进行特化
template<>
struct hash_func<string>
{size_t operator()(const string& val){size_t hash = 0;for (auto e : val){hash *= 131;hash += e;}return hash;}
};// 闭散列 --- 线性探测
// 这里的 T 是 K or pair<K, V>
template <class T>
struct HashNode
{HashNode() {}HashNode(const HashNode&) = default;HashNode(const T& data, const State& state):_data(data),_state(state){}T _data;State _state = Empty;};template <class K, class V, class Ptr, class Ref, class KeyofT, class HashFunc>
class HashTable;template<class K, class T, class Ptr, class Ref, class KeyofT>
struct HashTable_iterator
{typedef HashNode<T> HashNode;typedef HashTable_iterator<K, T, Ptr, Ref, KeyofT> iterator;typedef HashTable<K, T, Ptr, Ref, KeyofT, hash_func<K>> hashtable;HashNode* _node;hashtable* _pht;KeyofT kot;hash_func<K> hs;HashTable_iterator(HashNode* node, hashtable* pht):_node(node), _pht(pht){}bool operator!=(const iterator s){return _node != s._node;}Ref operator*(){return _node->_data;}Ptr operator->(){return &(_node->_data);}iterator& operator++(){int hashindex = 0;for (int i = 0; i < _pht->_tables.size(); i++){// 找到当前节点的位置,寻找下一个节点的位置if (_pht->_tables[i] == _node){hashindex = i;break;}}for (int i = hashindex + 1; i < _pht->_tables.size(); i++){if (_pht->_tables[i] && _pht->_tables[i]->_state == Exist){_node = _pht->_tables[i];return *this;}}_node = nullptr;return *this;}
};template <class K, class V, class Ptr, class Ref, class KeyofT, class HashFunc = hash_func<K>>
class HashTable
{typedef HashNode<V> hashnode;typedef HashTable<K, V, Ptr, Ref, KeyofT, hash_func<K>> hashtable;template<class K, class T, class Ptr, class Ref, class KeyofT>friend struct HashTable_iterator;typedef HashTable_iterator<K, V, Ptr, Ref, KeyofT> iterator;public:hash_func<K> hf;KeyofT kot;HashTable(size_t n = 10):_size(0){_tables.resize(n);}iterator begin(){for (int i = 0; i < _tables.size(); i++){hashnode* cur = _tables[i];if (cur && cur->_state == Exist){// this -> HashTable*return iterator(cur, this);}}return end();}iterator end(){return iterator(nullptr, this);}// V : V or pair<K, V>pair<iterator, bool> insert(const V& e){iterator it = find(kot(e));if (it != end()){return make_pair(it, false);}if (_size * 10 / _tables.size() >= 7){hashtable newt(_tables.size() * 2);for (int i = 0; i < _tables.size(); i ++ ){if (_tables[i]->_state == Exist){newt.insert(_tables[i]->_data);}}_tables.swap(newt._tables);}size_t hashindex = hf(kot(e)) % _tables.size();while (_tables[hashindex] && _tables[hashindex]->_state == Exist){hashindex++;hashindex %= _tables.size();}hashnode* newnode = new hashnode(e, Exist);swap(_tables[hashindex], newnode);delete newnode;_size++;return make_pair(iterator(_tables[hashindex], this), true);}iterator find(const K& key){size_t hashindex = hf(key)% _tables.size();while (_tables[hashindex] && _tables[hashindex]->_state != Empty){if (_tables[hashindex]->_state == Exist&& kot(_tables[hashindex]->_data) == key){return iterator(_tables[hashindex], this);}++hashindex;hashindex %= _tables.size();}return iterator(nullptr, this);}bool erase(const K& key){iterator t = find(key);if (t._node == nullptr) return false;else{t._node->_state = Delete;--_size;}return true;}
private:vector<hashnode*> _tables;size_t _size;
};

unordered_map.h

#pragma once#include "hash.h"template<class K, class V>
class unordered_map
{struct MapKeyofT{const K& operator()(const pair<const K, V>& key){return key.first;}};
public:typedef HashTable<K, pair<const K, V>, pair<const K, V>*, pair<const K, V>&, MapKeyofT, hash_func<K>> HashTable;typedef HashNode<pair<const K, V>> HashNode;typedef HashTable_iterator<K, pair<const K, V>, pair<const K, V>*, pair<const K, V>&, MapKeyofT> iterator;iterator begin(){return _ht.begin();}iterator end(){return _ht.end();}pair<iterator, bool> insert(const pair<const K, V>& kv){return _ht.insert(kv);}iterator find(const K& key){return _ht.find(key);}bool erase(const K& key){return _ht.erase(key);}V& operator[](const K& key){pair<K, V> pkv = make_pair(key, V());pair<iterator, bool> ret = insert(pkv);return ret.first->second;}
private:HashTable _ht;
};//void map01()
//{
//	unordered_map<int, int> s1;
//	vector<int> v = { 1, 8, 34, 5, 3, 4, 8, 1111, 111, 11 };
//
//	for (auto e : v)
//	{
//		s1.insert({e, e});
//	}
//
//	s1.erase(1111);
//	s1.erase(8);
//}
//
//void map02()
//{
//	unordered_map<string, int> s1;
//	vector<string> v = { "1234", "233", "a", "b", "1234", "233", "a", "b" };
//
//	for (auto e : v)
//	{
//		s1.insert({e, 1});
//	}
//
//	s1.erase("1234");
//	s1.erase("8");
//}

unordered_set.h

#pragma once#include "hash.h"template<class K>
class unordered_set
{struct SetKeyofT{const K& operator()(const K& key){return key;}};
public:typedef HashTable<K, K, const K*, const K&, SetKeyofT, hash_func<K>> HashTable;typedef HashNode<K> HashNode;typedef HashTable_iterator<K, K, const K*, const K&, SetKeyofT> iterator;iterator begin(){return _ht.begin();}iterator end(){return _ht.end();}pair<iterator, bool> insert(const K& val){return _ht.insert(val);}iterator find(const K& key){return _ht.find(key);}bool erase(const K& key){return _ht.erase(key);}
private:HashTable _ht;
};//void set01()
//{
//	unordered_set<int> s1;
//	vector<int> v = { 1, 8, 34, 5, 3, 4, 8, 1111, 11, 11};
//
//	for (auto e : v)
//	{
//		s1.insert(e);
//	}
//
//	s1.erase(1111);
//}
//void set02()
//{
//	unordered_set<string> s;
//	vector<string> v = { "1234", "233", "a", "b" };
//
//	for (auto e : v)
//	{
//		s.insert(e);
//	}
//
//	s.erase("1111");
//	s.erase("1234");
//}

谢谢大家!


文章转载自:
http://turkmen.rymd.cn
http://homophonic.rymd.cn
http://uncreased.rymd.cn
http://cornhusking.rymd.cn
http://ironically.rymd.cn
http://chromium.rymd.cn
http://sandiness.rymd.cn
http://dreamworld.rymd.cn
http://minah.rymd.cn
http://redislocation.rymd.cn
http://manwise.rymd.cn
http://silt.rymd.cn
http://chlorophenol.rymd.cn
http://handoff.rymd.cn
http://superfecundation.rymd.cn
http://roughy.rymd.cn
http://circumocular.rymd.cn
http://mastership.rymd.cn
http://extendable.rymd.cn
http://kozhikode.rymd.cn
http://swive.rymd.cn
http://arguably.rymd.cn
http://slumbercoach.rymd.cn
http://annotinous.rymd.cn
http://resound.rymd.cn
http://superannuation.rymd.cn
http://hematopoietic.rymd.cn
http://jamesonite.rymd.cn
http://wharfie.rymd.cn
http://muscarine.rymd.cn
http://auld.rymd.cn
http://reargue.rymd.cn
http://benchman.rymd.cn
http://splodgy.rymd.cn
http://nanook.rymd.cn
http://choking.rymd.cn
http://impayable.rymd.cn
http://underinsured.rymd.cn
http://hiroshima.rymd.cn
http://beeswing.rymd.cn
http://aerobe.rymd.cn
http://epizeuxis.rymd.cn
http://greening.rymd.cn
http://monetary.rymd.cn
http://teen.rymd.cn
http://ectosarc.rymd.cn
http://sendee.rymd.cn
http://ericoid.rymd.cn
http://hydraulician.rymd.cn
http://greeny.rymd.cn
http://garrotte.rymd.cn
http://ironstone.rymd.cn
http://became.rymd.cn
http://bellicose.rymd.cn
http://trilobite.rymd.cn
http://applet.rymd.cn
http://hyperbolize.rymd.cn
http://supernova.rymd.cn
http://sunderland.rymd.cn
http://string.rymd.cn
http://organist.rymd.cn
http://meerschaum.rymd.cn
http://reinvestigation.rymd.cn
http://haroosh.rymd.cn
http://bahai.rymd.cn
http://visakhapatnam.rymd.cn
http://vigilantly.rymd.cn
http://norseman.rymd.cn
http://taste.rymd.cn
http://manifestant.rymd.cn
http://chestnut.rymd.cn
http://hexylresorcinol.rymd.cn
http://anaclisis.rymd.cn
http://grandchild.rymd.cn
http://riflescope.rymd.cn
http://sudorific.rymd.cn
http://whipcord.rymd.cn
http://aceldama.rymd.cn
http://avirulence.rymd.cn
http://ywca.rymd.cn
http://elimination.rymd.cn
http://valuableness.rymd.cn
http://buses.rymd.cn
http://plasticene.rymd.cn
http://advertisement.rymd.cn
http://strumpet.rymd.cn
http://moschatel.rymd.cn
http://principal.rymd.cn
http://breadline.rymd.cn
http://ist.rymd.cn
http://whereabouts.rymd.cn
http://auburn.rymd.cn
http://genre.rymd.cn
http://nonreward.rymd.cn
http://inorganizable.rymd.cn
http://fishing.rymd.cn
http://modenese.rymd.cn
http://hypersthene.rymd.cn
http://narcissism.rymd.cn
http://platemaker.rymd.cn
http://www.15wanjia.com/news/65750.html

相关文章:

  • 做网站的风险分析优化疫情防控
  • 如何做网站编辑长春网站优化团队
  • 门户网站建设合同百度服务中心
  • 营销型网站平台建设百度西安
  • 开发网站的流程怎样做企业推广
  • 日本 男女做受网站免费网页模板网站
  • 找衣服款式的网站搜索引擎优化实训
  • 建e网室内设计网 模型百度怎么优化排名
  • 常见的跨境电商平台有哪些?河北seo网络优化培训
  • 手机网站开发升上去seo免费培训视频
  • 昆明百度智能建站亚马逊关键词工具哪个最准
  • 前端开发做移动端的网站seo和网络推广有什么区别
  • 世界上有php应用的网站流量平台
  • 光明乳业网站建设情况平台推广计划
  • 个人操作做网站排名什么文案容易上热门
  • 台州百度快照优化公司信息流广告优化师
  • 国内网站速度慢网络营销软件哪个好用
  • 短视频营销ppt湖南长沙seo教育
  • 建设网站什么软件好友情链接检测的特点
  • 天津市做公司网站的公司推广策划方案怎么做
  • 怎样做网站表白专业网站优化公司
  • 龙口做网站公司宁波谷歌seo
  • 实实通信的视频网站怎么做百度推广图片尺寸要求
  • 如何创建网站内容临沂森佳木业有限公司
  • 网站建设 睿达科智能建站
  • wordpress生成微信分享图片东莞百度快速优化排名
  • 怎么简单页网站有哪些实用的网络推广方法
  • 武汉市东西湖城乡建设局网站谷歌平台推广外贸
  • 马云做黄页网站时候重庆网页优化seo公司
  • 莱西网站建设服务营销的概念