博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hashmap C++实现
阅读量:4510 次
发布时间:2019-06-08

本文共 3363 字,大约阅读时间需要 11 分钟。

 

hashmap.h

#ifndef _HASHMAP_H_#define _HASHMAP_H_template
class HashNode{public: Key _key; Value _value; HashNode *next; HashNode(Key key, Value value) { _key = key; _value = value; next = NULL; } ~HashNode(){} HashNode& operator=(const HashNode& node) { _key = node._key; _value = node._value; next = node.next; return *this; }};template
class HashMap{public: HashMap(int size); ~HashMap(); bool insert(const Key& key, const Value& value); bool del(const Key& key); Value& find(const Key& key); Value& operator [](const Key& key);private: HashFunc hash; EqualKey equal; HashNode
**table; unsigned int _size; Value ValueNULL;};template
HashMap
::HashMap(int size) : _size(size),hash(),equal(){ table = new HashNode
*[_size]; for (unsigned i = 0; i < _size; i++) table[i] = NULL;}template
HashMap
::~HashMap(){ for (unsigned i = 0; i < _size; i++) { HashNode
*currentNode = table[i]; while (currentNode) { HashNode
*temp = currentNode; currentNode = currentNode->next; delete temp; } } delete table;}template
bool HashMap
::insert(const Key& key, const Value& value){ int index = hash(key)%_size; HashNode
* node = new HashNode
(key,value); node->next = table[index]; table[index] = node; return true;}template
bool HashMap
::del(const Key& key){ unsigned index = hash(key) % _size; HashNode
* node = table[index]; HashNode
* prev = NULL; while (node) { if (node->_key == key) { if (prev == NULL) { table[index] = node->next; } else { prev->next = node->next; } delete node; return true; } prev = node; node = node->next; } return false;}template
Value& HashMap
::find(const Key& key){ unsigned index = hash(key) % _size; if (table[index] == NULL) return ValueNULL; else { HashNode
* node = table[index]; while (node) { if (node->_key == key) return node->_value; node = node->next; } }}template
Value& HashMap
::operator [](const Key& key){ return find(key);}#endif

测试:

//首先要定义hash函数与比较函数class HashFunc{public:    int operator()(const std::string& key )    {        int hash = 0;        for(int i = 0; i < key.length(); ++i)        {            hash = hash << 7 ^ key[i];        }        return (hash & 0x7FFFFFFF);    }};class EqualKey{public:    bool operator()(const std::string& A, const std::string& B)    {        if (A.compare(B) == 0)            return true;        else            return false;    }};//测试用例int main(){    HashMap
hashmap(100); hashmap.insert("hello", "world"); hashmap.insert("why", "dream"); hashmap.insert("c++", "good"); hashmap.insert("welcome", "haha"); std::cout << "after insert:" << std::endl; std::cout << hashmap.find("welcome").c_str() << std::endl; std::cout << hashmap.find("c++").c_str() << std::endl; std::cout << hashmap["why"].c_str() << std::endl; std::cout << hashmap["hello"].c_str() << std::endl; if (hashmap.del("hello")) std::cout << "remove is ok" << std::endl; //remove is ok std::cout << hashmap.find("hello").c_str() << std::endl; //not exist print NULL hashmap["why"] = "love"; std::cout << hashmap["why"].c_str() << std::endl; return 0;}

原作者:

 

转载于:https://www.cnblogs.com/evenleee/p/11345974.html

你可能感兴趣的文章
mysql 行转列 和 列转行
查看>>
[Leetcode]
查看>>
再谈vertical-align与line-height
查看>>
有关时延扩展的双语句子
查看>>
工作多年后积累的设计灵活,稳定,优秀WinForms应用程序的最佳实践 WinForms best practice...
查看>>
iOS开发——高级篇——iOS键盘的相关设置(UITextfield)
查看>>
JVMGC机制
查看>>
IAR for AVR 报array is too large错误 【已解决】
查看>>
老子《道德经》第六十二章
查看>>
Junit问题01 利用 @Autowired 注入失效问题
查看>>
连通块
查看>>
servlet.txt笔记
查看>>
jquery设置select选中
查看>>
今天说一下DML触发器的顺序
查看>>
Memcached学习(一)--网络模型
查看>>
FragmentTransaction add 和 replace 区别 转
查看>>
jQuery 效果方法
查看>>
STM32物联网通信WIFI
查看>>
java反射案例详解
查看>>
MAGENTO 与 reindexer
查看>>