西部中大建设集团有限公司网站怎样建设一个自己的网站微商
西部中大建设集团有限公司网站,怎样建设一个自己的网站微商,销售网站开发实践报告,如何自己建设简单的手机网站目录
前言
一、修改kv模型为data模型
1.添加MyUnorderedSet.h和MyUnorderedMap.h
2.修改HashNode
3.修改HashTable
二、普通迭代器
三、const迭代器
四、unordered_map重载operator[]
总结 前言 在上一篇文章中#xff0c;我们手写了一份哈希表我们手写了一份哈希表也确实实现了插入删除查找等功能但是我们只写了一份“KeyValue”模型的哈希表并没有“Key”模型的同时我们也没有迭代器遍历的功能因此我们现在要用写好的哈希表去模仿库里面的unordered_ser和unordered_map。 本文是根据这个哈希表进行的修改添加具体代码如下
HashTable.h
#pragma once
#includevectortemplateclass K
struct HashFunc
{size_t operator()(const K key){return (size_t)key;}
};template
struct HashFuncstring
{size_t operator()(const string s){size_t sum 0;for (auto e : s){sum * 31;sum e;}return sum;}
};
namespace kky_open_address
{enum Status{EMPTY,EXIST,DELETE,};templateclass K, class Vstruct HashDate{pairK, V _kv;Status _s;};templateclass K,class V ,class Hash HashFuncKclass HashTable{public:HashTable(){_tables.resize(10);}Hash hs;HashDateK,V* Find(const K key){size_t hashi hs(key) % _tables.size();while (_tables[hashi]._s ! EMPTY){if (_tables[hashi]._s ! DELETE _tables[hashi]._kv.first key)return _tables[hashi];hashi;hashi % _tables.size();}return nullptr;}bool Erase(const K key){HashDateK, V* ret Find(key);if (ret){ret-_s DELETE;--_n;return true;}return false;}bool Insert(const pairK,V kv){if (Find(kv.first)){return false;}if (_n*10 / _tables.size() 7){int newcapacity _tables.size() * 2;HashTableK, V newHT;newHT._tables.resize(newcapacity);for (size_t i 0; i _tables.size(); i){if(_tables[i]._s EXIST){newHT.Insert(_tables[i]._kv);}}_tables.swap(newHT._tables);}size_t hashi hs(kv.first) % _tables.size();while (_tables[hashi]._s EXIST){hashi;hashi % _tables.size();}_tables[hashi]._kv kv;_tables[hashi]._s EXIST;_n;return true;}void Print(){for (size_t i 0; i _tables.size(); i){if (_tables[i]._s EXIST){//printf([%d]-%d:%s\n, i,_tables[i]._kv.first, EXIST);cout [ i ]- _tables[i]._kv.first: _tables[i]._kv.second endl;}else if(_tables[i]._s EMPTY){//printf([%d]-%d:%s\n, i, _tables[i]._kv.first, EMPTY);cout [ i ]- endl;}else{//printf([%d]-%d:%s\n, i, _tables[i]._kv.first, DELETE);cout [ i ]- DELETE endl;}}cout endl;}private:vectorHashDateK,V _tables;size_t _n;};void test01(){HashTableint, int ht;int arr[] { 3,13,23,4,5,14,7,13 };for (auto e : arr){ht.Insert(make_pair(e, e));}ht.Print();ht.Erase(13);ht.Print();ht.Insert(make_pair(33,33));ht.Print();}void test02(){string arr[] { 香蕉,苹果,橘子,香蕉,苹果 ,香蕉,苹果 ,香蕉 };HashTablestring, int ht;for (auto e : arr){HashDatestring, int* ret ht.Find(e);if(ret)ret-_kv.second;elseht.Insert(make_pair(e, 1));}ht.Print();}
}namespace kky_hash_bucket
{templateclass K, class Vstruct HashNode{pairK, V _kv;HashNodeK, V* _next;HashNode(const pairK,V kv):_kv(kv),_next(nullptr){}};templateclass K, class V, class Hash HashFuncKclass HashTable{typedef HashNodeK, V Node;public:HashTable(){_tables.resize(10);}HashTable(const HashTableK,V ht){_tables.resize(ht._tables.size(), nullptr);for (size_t i 0; i ht._tables.size(); i){Node* cur ht._tables[i];while (cur){Node* newnode new Node(cur-_kv);if (_tables[i]nullptr){_tables[i] newnode;}else{newnode-_next _tables[i];_tables[i] newnode;}cur cur-_next;}}}HashTableK, V operator(HashTableK, V ht){_tables.swap(ht._tables);swap(_n, ht._n);return *this;}~HashTable(){for (size_t i 0; i _tables.size(); i){Node* cur _tables[i];while (cur){Node* next cur-_next;delete cur;cur next;}_tables[i] nullptr;}}bool Insert(const pairK, V kv){Hash hs;if (Find(kv.first))return false;if (_n _tables.size()){vectorNode* newtables;newtables.resize(_tables.size() * 2, nullptr);for (size_t i 0; i _tables.size(); i){Node* cur _tables[i];while (cur){Node* next cur-_next;//映射到新表size_t hashi hs(cur-_kv.first) % newtables.size();cur-_next newtables[hashi];newtables[hashi] cur;cur next;}//置空防止析构出现问题_tables[i] nullptr;}_tables.swap(newtables);}size_t hashi hs(kv.first) % _tables.size();Node* newnode new Node(kv);newnode-_next _tables[hashi];_tables[hashi] newnode;_n;return true;}Node* Find(const K key){Hash hs;size_t hashi hs(key) % _tables.size();Node* cur _tables[hashi];while (cur){if (cur-_kv.first key){return cur;}cur cur-_next;}return nullptr;}bool Erase(const K key){Hash hs;size_t hashi hs(key) % _tables.size();Node* cur _tables[hashi];Node* prev nullptr;while (cur){if (cur-_kv.first key){if (prev nullptr){_tables[hashi] cur-_next;}else{prev-_next cur-_next;}delete cur;return true;}prev cur;cur cur-_next;}return false;}void Print(){for (size_t i 0; i _tables.size(); i){Node* cur _tables[i];cout [ i ] 挂载-;while (cur){cout cur-_kv.first : cur-_kv.second -;cur cur-_next;}cout endl;}cout endl;}void Some(){size_t bucketSize 0;size_t maxBucketLen 0;double averageBucketLen 0;for (size_t i 0; i _tables.size(); i){Node* cur _tables[i];if (cur){bucketSize;}size_t bucketLen 0;while (cur){bucketLen;cur cur-_next;}if (bucketLen maxBucketLen){maxBucketLen bucketLen;}}averageBucketLen (double)bucketSize / (double)_n;printf(all bucketSize:%d\n,_tables.size());printf(bucketSize:%d\n, bucketSize);printf(maxBucketLen:%d\n, maxBucketLen);printf(averageBucketLen:%lf\n\n, averageBucketLen);}private:vectorNode* _tables;size_t _n;};void test01(){HashTableint, int ht;int arr[] { 3,13,23,4,5,14,33,34,43,44 };for (auto e : arr){ht.Insert(make_pair(e, e));}ht.Print();cout ht.Find(43) endl;ht.Erase(43);ht.Print();cout ht.Find(43) endl;}void test02(){string arr[] { 香蕉,苹果,橘子,香蕉,苹果 ,香蕉,苹果 ,香蕉 };HashTablestring, int ht;for (auto e : arr){HashNodestring, int* ret ht.Find(e);if (ret)ret-_kv.second;elseht.Insert(make_pair(e, 1));}}void test03(){const size_t N 1000000;unordered_setint us;setint s;HashTableint, int ht;vectorint v;v.reserve(N);srand(time(0));for (size_t i 0; i N; i){//v.push_back(rand()); // N比较大时重复值比较多v.push_back(rand() i); // 重复值相对少//v.push_back(i); // 没有重复有序}size_t begin1 clock();for (auto e : v){s.insert(e);}size_t end1 clock();cout set insert: end1 - begin1 endl;size_t begin2 clock();for (auto e : v){us.insert(e);}size_t end2 clock();cout unordered_set insert: end2 - begin2 endl;size_t begin10 clock();for (auto e : v){ht.Insert(make_pair(e, e));}size_t end10 clock();cout HashTbale insert: end10 - begin10 endl endl;size_t begin3 clock();for (auto e : v){s.find(e);}size_t end3 clock();cout set find: end3 - begin3 endl;size_t begin4 clock();for (auto e : v){us.find(e);}size_t end4 clock();cout unordered_set find: end4 - begin4 endl;size_t begin11 clock();for (auto e : v){ht.Find(e);}size_t end11 clock();cout HashTable find: end11 - begin11 endl endl;cout 插入数据个数 us.size() endl endl;ht.Some();size_t begin5 clock();for (auto e : v){s.erase(e);}size_t end5 clock();cout set erase: end5 - begin5 endl;size_t begin6 clock();for (auto e : v){us.erase(e);}size_t end6 clock();cout unordered_set erase: end6 - begin6 endl;size_t begin12 clock();for (auto e : v){ht.Erase(e);}size_t end12 clock();cout HashTable Erase: end12 - begin12 endl endl;}
} 我们也借鉴了红黑树封装set和map的思想来进行封装。画图还算比较详细没看过最好可以小看一下不然会有点难懂 大厦并不是一下子建成我们在封装过程中需要缝缝补补这也是为什么我们看库里面的文件一时间难以看懂因为库里面的也是一步一步封装好的。那接下来让我们开始封装吧
一、修改kv模型为data模型
一样的我们要将“KeyValue”模型普适化让这一份代码既可以封装“Key”模型也可以封装“KeyValue”模型。
1.添加MyUnorderedSet.h和MyUnorderedMap.h
HashTable的第一个参数是Key无需多说“Key”模型和“KeyValue”模型都需要Key。
让第二个参数来决定到底是“Key”模型还是“KeyValue”模型。“Key”模型第二个参数也是Key“KeyValue”第二个参数是pairconst K,V。加const目的是让K无法修改
同时将Hash函数放在unordered_set和unordered_map里因为我们封装的目的就是让你去调用这两个类而不是去调用HashTable因此你应该在这里传递Hash函数并给缺省值。
MyUnorderedSet.h添加
#pragma once
#includeHashTable.hnamespace kky
{templateclass K, class Hash HashFuncKclass unordered_set{private:kky_hash_bucket::HashTableK, K, Hash ht;};
}
MyUnorderedMap.h添加
#pragma once
#includeHashTable.hnamespace kky
{templateclass K,class V,class Hash HashFuncKclass unordered_map{private:kky_hash_bucket::HashTableK, pairconst K, V, Hash ht;};
}
2.修改HashNode 首先将HashNode模板参数改成T你传递的T是“pairK,V”那我里面的数据_data就是pairK,V你传递T的是“Key”数据_data也是Key。 3.修改HashTable
模板类型改成T同时修改下面的类型该给T就给T。 但我们仅仅修改这个T也是无法达到想要的效果。比如find函数我们传递的类型是单值key通过key判断在不在你如果是kv模型就会存在问题所以我们应该添加上仿函数帮助我们处理。 unordered_set里添加SetKeyOfT走个过场取出key并将类型传递给HashTable当做第三参数 unordered_map里添加MapKeyOfT取出kv中的key传递给HashTable当做第三参数。 在HashTable也添加KeyOfT后续构建的地方也都添加这个类。如下 用KeyOfT构建对象无论是k还是kv利用仿函数就可以取出存在data里面的key了。 后续的地方都可以这样取出为了方便观看这里就不多做展示了。
最后使用代码测试一下set测试 map测试 没问题成功的用一张哈希表构建出了k模型和kv模型。
二、普通迭代器
现在我们要封装迭代器了但是迭代器的还算是个问题。
如下图如果当前结点是4那么我们很容易找到他的下一个结点为14但如果一直到44呢44的下一个应该是5了那我们如何知道当前的索引hashi为多少呢并且我们哈希表也没有就算知道了索引hashi也没办法计算。 因此至少我们得将哈希表的地址传递给iterator当做参数只有能够访问到哈希表才可以进行迭代器的。 那么hashi我们要不要传如果传就可以直接用如果不传我们也可以通过哈希表来计算。因为迭代器我们并不会生成很多个大部分的使用场景都是只使用O1个迭代器帮我们遍历。这里为了效率我们牺牲了一点点空间我们选择传递hashi。 那么我们现在就有三个参数一个结点node一个哈希表pht还有一个索引hashi。 并写出构造函数、operator、operator!、operator*、operator- 。 operator!、operator*、operator- 都很简单不多赘述。 对于operator如果当前节点的_next存在就走到_next即可若不存在根据hashi一直走到结点存在的地方_node _pht-_tables[_hashi]; 如果hashi_tables.size()也就是走到了末尾还没遇到不为空的结点证明后续没有结点了就将_node给到nullptr最后return *this; 代码如下
templateclass K, class T, class KeyOfT, class Hash
struct __HTIterator
{typedef HashNodeT Node;typedef __HTIteratorK, T, KeyOfT, Hash Self;HashTableK, T, KeyOfT, Hash* _pht;Node* _node;size_t _hashi;__HTIterator(Node* node, HashTableK, T, KeyOfT, Hash* pht,size_t hashi):_node(node),_pht(pht),_hashi(hashi){}Self operator(){if (_node-_next){_node _node-_next;}else{while (_hashi _pht-_tables.size()){if (_pht-_tables[_hashi]){_node _pht-_tables[_hashi];break;}}if (_hashi _pht-_tables.size()){_node nullptr;}}return *this;}bool operator!(const Self s){return _node ! s._node;}
};
后续我们就在HashTable里面添加iterator的begin()和end()同时再unordered_map和unordered_set里复用一下迭代器就可以了。
HashTable添加如下代码 unordered_set添加 为什么要用typename因为走到当前iterator的时候哈希表里面的iterator还没有实例化加了typename告诉编译器你先别急等后面实例化之后再去找iterator 在红黑树封装map和set有更详细的介绍。 unordered_map添加 我们用如下代码测试一下 报错了看下面的第二张图我们发现_pht根本找不到这是为什么呢 这是因为编译器会往上找并不会往下找目前的_HTIterator和HashTable他们两个存在相互依赖的关系HashTable要用_HTIterator里面的iterator,_HTIterator要用HashTable这一张表那么我们应该如何处理呢 第一个方法是不要传递HashTable传递HashTable里面的vector因为我们只使用到了HashTable里面的那个vector来进行遍历实现操作里面需要。这是一个办法。第二个方法是前置声明告诉编译器HashTable在下面你去下面找。 这里我们选择了第二种方法目的是让我们可以更好的掌握前置声明还有友元等下会提到 那么我们只需要在前面添加上声明即可。 添加前置声明后再次编译又报错了看下面第二张图报错提示告诉我们_pht里_tables是私有成员外部无法访问。 这就可以用到C友元的概念了在A中写友元B代表B是A的友元B类里面可以访问A类私有成员。那么我想让__HTIterator访问HashTable的私有成员那么就应该在HashTable里面写友元__HTIterator。 HashTable添加如下代码即可 现在就没有问题了。 三、const迭代器
老生常谈了const迭代器只有这两个地方改为const就可以了。 HashTable中iterator和const_iterator模板参数为 再如下修改一下即可 再添加一份const_iterator的begin()和end()方便调用 unordered_set修改如下因为set的特性是只有key因此无论iterator还是const_iterator我们都不要修改因此typedef都为const_iterator也就是无论iterator还是const_iterator本质都为const_iterator unordered_map需要普通的迭代器不能修改keyconst的迭代器kv都不能修改因此是正常操作代码如下 完成修改后我们就按照之前的代码运行一下看看普通的有没有bug发现报错了这是为什么呢 分析情况如下你构建const迭代器时传递的this被const修饰过由于构造参数写的是普通的因此传参发生了权限扩大就报错了 修改如下添加上const关键字同时也要在变量_pht的地方也用const修饰不然用const的赋值给普通的也是不可以的。 再次运行就没问题了。 切换为const迭代器再做测试。 我们在__HTIterator写一个拷贝构造函数支持从普通的转为const的就可以了。 最后测试没问题了。 四、unordered_map重载operator[]
要将Insert返回类型从bool转化为iterator 为什么可以去红黑树封装set和map查看 同时Find函数也应该修改需要返回一个迭代器 修改如下 Insert函数修改 unordered_set修改 unordered_map也修改 重载operator[]为什么返回的是ret.frist-second呢
ret.frist是iterator我们iterator里面重载了operator-这样就可以去到_data数据的地址了再-second就可以取到第二个数据这里编译器帮我们简化了因此可以少些一个- 实际上应该这样写但是太不美观了为了可读性采用上面的写法。 最后测试一下没问题。 总结 我们修改了很多地方一直在缝缝补补为什么不一下子就弄好呢因为我刚学会走路你偏要我去跨栏那我不得先把走路联系好再学跑步最后再去跨栏嘛一步一步虽然慢但是胜在稳健封装也是如此。 最后附上总代码
HashTable.h
#pragma once
#includevectortemplateclass K
struct HashFunc
{size_t operator()(const K key){return (size_t)key;}
};template
struct HashFuncstring
{size_t operator()(const string s){size_t sum 0;for (auto e : s){sum * 31;sum e;}return sum;}
};
namespace kky_open_address
{enum Status{EMPTY,EXIST,DELETE,};templateclass K, class Vstruct HashDate{pairK, V _kv;Status _s;};templateclass K, class V, class Hash HashFuncKclass HashTable{public:HashTable():_n(0){_tables.resize(10);}Hash hs;HashDateK, V* Find(const K key){size_t hashi hs(key) % _tables.size();while (_tables[hashi]._s ! EMPTY){if (_tables[hashi]._s ! DELETE _tables[hashi]._kv.first key)return _tables[hashi];hashi;hashi % _tables.size();}return nullptr;}bool Erase(const K key){HashDateK, V* ret Find(key);if (ret){ret-_s DELETE;--_n;return true;}return false;}bool Insert(const pairK, V kv){if (Find(kv.first)){return false;}if (_n * 10 / _tables.size() 7){size_t newcapacity _tables.size() * 2;HashTableK, V newHT;newHT._tables.resize(newcapacity);for (size_t i 0; i _tables.size(); i){if (_tables[i]._s EXIST){newHT.Insert(_tables[i]._kv);}}_tables.swap(newHT._tables);}size_t hashi hs(kv.first) % _tables.size();while (_tables[hashi]._s EXIST){hashi;hashi % _tables.size();}_tables[hashi]._kv kv;_tables[hashi]._s EXIST;_n;return true;}void Print(){for (size_t i 0; i _tables.size(); i){if (_tables[i]._s EXIST){//printf([%d]-%d:%s\n, i,_tables[i]._kv.first, EXIST);cout [ i ]- _tables[i]._kv.first : _tables[i]._kv.second endl;}else if (_tables[i]._s EMPTY){//printf([%d]-%d:%s\n, i, _tables[i]._kv.first, EMPTY);cout [ i ]- endl;}else{//printf([%d]-%d:%s\n, i, _tables[i]._kv.first, DELETE);cout [ i ]- DELETE endl;}}cout endl;}private:vectorHashDateK, V _tables;size_t _n;};void test01(){HashTableint, int ht;int arr[] { 3,13,23,4,5,14,7,13 };for (auto e : arr){ht.Insert(make_pair(e, e));}ht.Print();ht.Erase(13);ht.Print();ht.Insert(make_pair(33, 33));ht.Print();}void test02(){string arr[] { 香蕉,苹果,橘子,香蕉,苹果 ,香蕉,苹果 ,香蕉 };HashTablestring, int ht;for (auto e : arr){HashDatestring, int* ret ht.Find(e);if (ret)ret-_kv.second;elseht.Insert(make_pair(e, 1));}ht.Print();}
}namespace kky_hash_bucket
{templateclass Tstruct HashNode{T _data;HashNodeT* _next;HashNode(const T data):_data(data), _next(nullptr){}};templateclass K, class T, class KeyOfT, class Hashclass HashTable;templateclass K, class T, class Ref, class Ptr, class KeyOfT, class Hashstruct __HTIterator{typedef HashNodeT Node;typedef __HTIteratorK, T,Ref, Ptr, KeyOfT, Hash Self;typedef __HTIteratorK, T, T, T*, KeyOfT, Hash iterator;const HashTableK, T, KeyOfT, Hash* _pht;Node* _node;size_t _hashi;__HTIterator(Node* node,const HashTableK, T, KeyOfT, Hash* pht,size_t hashi):_node(node),_pht(pht),_hashi(hashi){}__HTIterator(const iterator it):_node(it._node),_pht(it._pht),_hashi(it._hashi){}Self operator(){if (_node-_next){_node _node-_next;}else{while (_hashi _pht-_tables.size()){if (_pht-_tables[_hashi]){_node _pht-_tables[_hashi];break;}}if (_hashi _pht-_tables.size()){_node nullptr;}}return *this;}Ref operator*(){return _node-_data;}Ptr operator-(){return _node-_data;}bool operator!(const Self s){return _node ! s._node;}};templateclass K, class T,class KeyOfT, class Hashclass HashTable{templateclass K, class T, class Ref, class Ptr, class KeyOfT, class Hashfriend struct __HTIterator;typedef HashNodeT Node;public:typedef __HTIteratorK, T, T, T*, KeyOfT, Hash iterator;typedef __HTIteratorK, T,const T,const T*, KeyOfT, Hash const_iterator;iterator begin(){for (size_t i 0; i _tables.size(); i){if(_tables[i]){return iterator(_tables[i], this, i);}}return end();}iterator end(){return iterator(nullptr, this, -1);}const_iterator begin() const{for (size_t i 0; i _tables.size(); i){if (_tables[i]){return const_iterator(_tables[i], this, i);}}return end();}const_iterator end() const{return const_iterator(nullptr, this, -1);}HashTable():_n(0){_tables.resize(10);}HashTable(const HashTableK, T,KeyOfT, Hash ht){_tables.resize(ht._tables.size(), nullptr);for (size_t i 0; i ht._tables.size(); i){Node* cur ht._tables[i];while (cur){Node* newnode new Node(cur-_kv);if (_tables[i] nullptr){_tables[i] newnode;}else{newnode-_next _tables[i];_tables[i] newnode;}cur cur-_next;}}}HashTableK, T,KeyOfT,Hash operator(HashTableK, T, KeyOfT, Hash ht){_tables.swap(ht._tables);swap(_n, ht._n);return *this;}~HashTable(){for (size_t i 0; i _tables.size(); i){Node* cur _tables[i];while (cur){Node* next cur-_next;delete cur;cur next;}_tables[i] nullptr;}}pairiterator, bool Insert(const T data){Hash hs;KeyOfT koft;iterator it Find(koft(data));if (it._node)return make_pair(it, false);if (_n _tables.size()){vectorNode* newtables;newtables.resize(_tables.size() * 2, nullptr);for (size_t i 0; i _tables.size(); i){Node* cur _tables[i];while (cur){Node* next cur-_next;//映射到新表size_t hashi hs(koft(cur-_data)) % newtables.size();cur-_next newtables[hashi];newtables[hashi] cur;cur next;}//置空防止析构出现问题_tables[i] nullptr;}_tables.swap(newtables);}size_t hashi hs(koft(data)) % _tables.size();Node* newnode new Node(data);newnode-_next _tables[hashi];_tables[hashi] newnode;_n;return make_pair(iterator(newnode, this, hashi), true);}iterator Find(const K key){Hash hs;KeyOfT koft;size_t hashi hs(key) % _tables.size();Node* cur _tables[hashi];while (cur){if (koft(cur-_data) key){return iterator(cur, this, hashi);}cur cur-_next;}return iterator(nullptr, this, -1);}bool Erase(const K key){Hash hs;size_t hashi hs(key) % _tables.size();Node* cur _tables[hashi];Node* prev nullptr;while (cur){if (cur-_kv.first key){if (prev nullptr){_tables[hashi] cur-_next;}else{prev-_next cur-_next;}delete cur;return true;}prev cur;cur cur-_next;}return false;}void Print(){for (size_t i 0; i _tables.size(); i){Node* cur _tables[i];cout [ i ] 挂载 -;while (cur){cout cur-_kv.first : cur-_kv.second -;cur cur-_next;}cout endl;}cout endl;}void Some(){size_t bucketSize 0;size_t maxBucketLen 0;double averageBucketLen 0;for (size_t i 0; i _tables.size(); i){Node* cur _tables[i];if (cur){bucketSize;}size_t bucketLen 0;while (cur){bucketLen;cur cur-_next;}if (bucketLen maxBucketLen){maxBucketLen bucketLen;}}averageBucketLen (double)bucketSize / (double)_n;printf(all bucketSize:%d\n, _tables.size());printf(bucketSize:%d\n, bucketSize);printf(maxBucketLen:%d\n, maxBucketLen);printf(averageBucketLen:%lf\n\n, averageBucketLen);}private:vectorNode* _tables;size_t _n;};
}
MyUnorderedSet.h
#pragma once
#includeHashTable.hnamespace kky
{templateclass K, class Hash HashFuncKclass unordered_set{struct SetKeyOfT{const K operator()(const K key){return key;}};public:typedef typename kky_hash_bucket::HashTableK, K, SetKeyOfT, Hash::const_iterator iterator;typedef typename kky_hash_bucket::HashTableK, K, SetKeyOfT, Hash::const_iterator const_iterator;const_iterator begin() const{return ht.begin();}const_iterator end() const{return ht.end();}pairiterator,bool Insert(const K key){return ht.Insert(key);}private:kky_hash_bucket::HashTableK, K, SetKeyOfT, Hash ht;};void set_test01(){unordered_setint s;s.Insert(1);s.Insert(555);s.Insert(55);s.Insert(5);unordered_setint::iterator it s.begin();while (it ! s.end()) {cout *it endl;it;}}
} MyUnorderedMap.h
#pragma once
#includeHashTable.hnamespace kky
{templateclass K,class V,class Hash HashFuncKclass unordered_map{struct MapKeyOfT{const K operator()(const pairconst K,V kv){return kv.first;}};public:typedef typename kky_hash_bucket::HashTableK, pairconst K, V, MapKeyOfT, Hash::iterator iterator;typedef typename kky_hash_bucket::HashTableK, pairconst K, V, MapKeyOfT, Hash::const_iterator const_iterator;iterator begin(){return ht.begin();}iterator end(){return ht.end();}const_iterator begin() const{return ht.begin();}const_iterator end() const{return ht.end();}pairiterator, bool Insert(const pairK,V kv){return ht.Insert(kv);}V operator[](const K key){pairiterator, bool ret ht.Insert(make_pair(key, V()));return ret.first.operator-()-second;}private:kky_hash_bucket::HashTableK, pairconst K, V, MapKeyOfT, Hash ht;};void map_test01(){unordered_mapstring, string dict;dict.Insert(make_pair(sort, 排序));dict.Insert(make_pair(left, 左边));dict.Insert(make_pair(right, 右边));unordered_mapstring, string::const_iterator it dict.begin();while (it ! dict.end()){/*it-second test;*/cout it-first:it-second endl;it;}}void map_test02(){string arr[] { 香蕉, 苹果,苹果, 苹果, 香蕉, 梨子, 香蕉, 梨子, 草莓, 梨子, 苹果, 苹果, 苹果 };unordered_mapstring, int count_map;for (auto e : arr){count_map[e];}for (auto kv : count_map){cout kv.first : kv.second endl;}cout endl;}
}
test.cpp
#includeiostream
#includestring
#includeset
#includeunordered_set
using namespace std;
#includeHashTable.h
#includeMyUnorderedMap.h
#includeMyUnorderedSet.h
int main()
{kky::set_test01();kky::map_test02();
} 谢谢大家观看
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/90256.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!