hashtable的C++实现
来源:互联网 发布:思念是一种病 知乎 编辑:程序博客网 时间:2024/05/22 10:27
#include <vector>#include <list>using namespace std;template <typename HashedObj>class HashTable{ public: explicit HashTable(int size = 101); void makeEmpty() { for(int i = 0; i < theLists.size(); i++) theLists[i].clear(); } bool contains(const HashedObj & x) const { const list<HashedObj> & whichList = theLists[myhash(x)]; return find(whichList.begin(), whichList.end(), x) != whichList.end(); } bool remove(const HashedObj & x) { list<HashedObj> & whichList = theLists[myhash(x)]; typename list<HashedObj>::iterator itr = find(whichList.begin(), whichList.end(), x); if(itr == whichList.end()) return false; whichList.erase(itr); --currentSize; return true; } bool insert(const HashedObj & x) { list<HashedObj> & whichList = theLists[myhash(x)]; if(find(whichList.begin(), whichList.end(), x) != whichList.end()) return false; whichList.push_back(x); if(++currentSize > theLists.size()) rehash(); return true; } private: vector<list<HashedObj> > theLists; // The array of Lists int currentSize; void rehash() { vector<list<HashedObj> > oldLists = theLists; // Create new double-sized, empty table theLists.resize(2 * theLists.size()); for(int j = 0; j < theLists.size(); j++) theLists[j].clear(); // Copy table over currentSize = 0; for(int i = 0; i < oldLists.size(); i++) { typename list<HashedObj>::iterator itr = oldLists[i].begin(); while(itr != oldLists[i].end()) insert(*itr++); } } int myhash(const HashedObj & x) const { int hashVal = hash(x); hashVal %= theLists.size(); if(hashVal < 0) hashVal += theLists.size(); return hashVal; }};
0 0
- HashTable的c语言实现
- C语言的HashTable简单实现
- HashTable的简单实现(C语言)
- C语言的HashTable简单实现
- C语言的HashTable简单实现
- C语言的HashTable简单实现
- C语言实现 HashTable
- c语言实现hashtable
- C语言实现HashTable
- C 语言模仿实现HashTable
- [C++]数据结构:散列表HashTable的实现与简单应用
- 简单的闭散列(hashtable)实现(c++)
- HashTable的实现
- HASHTABLE的内部实现
- Java的Hashtable实现
- Hashtable 的实现原理
- HashTable的实现测试
- HashTable的C++实现
- mycat 平台上实现将数据从oracle迁移到mysql
- 设计模式之 概览(设计模式是什么 设计模式分类)
- epoll用发
- Java IO流总结
- Thread详解5:synchronized的使用(一)
- hashtable的C++实现
- hdu5652 India and China Origins
- 12.结账流程(Checkout Process)
- ie兼容css-zoom作用
- POJ 2513(欧拉回路+ 并查集+字典树)
- IOS单例的实现
- IO五种模式
- 8. String to Integer (atoi)
- HDU 1166 敌兵布阵 线段树入门_单点更新