当前位置:   article > 正文

C++实现hashmap_c++中hash_map怎么写

c++中hash_map怎么写

由于hashmap不是c++ stl中标准实现,这样在跨平台使用时就可能会出现问题,于是想到自己实现一个hashmap

hash算法使用开链法解决hash冲突,主要实现了添加,删除,查找几个方法

头文件如下hashmap.h

  1. #ifndef _HASHMAP_H_
  2. #define _HASHMAP_H_
  3. template<class Key, class Value>
  4. class HashNode
  5. {
  6. public:
  7. Key _key;
  8. Value _value;
  9. HashNode *next;
  10. HashNode(Key key, Value value)
  11. {
  12. _key = key;
  13. _value = value;
  14. next = NULL;
  15. }
  16. ~HashNode()
  17. {
  18. }
  19. HashNode& operator=(const HashNode& node)
  20. {
  21. _key = node._key;
  22. _value = node._value;
  23. next = node.next;
  24. return *this;
  25. }
  26. };
  27. template <class Key, class Value, class HashFunc, class EqualKey>
  28. class HashMap
  29. {
  30. public:
  31. HashMap(int size);
  32. ~HashMap();
  33. bool insert(const Key& key, const Value& value);
  34. bool del(const Key& key);
  35. Value& find(const Key& key);
  36. Value& operator [](const Key& key);
  37. private:
  38. HashFunc hash;
  39. EqualKey equal;
  40. HashNode<Key, Value> **table;
  41. unsigned int _size;
  42. Value ValueNULL;
  43. };
  44. template <class Key, class Value, class HashFunc, class EqualKey>
  45. HashMap<Key, Value, HashFunc, EqualKey>::HashMap(int size) : _size(size),hash(),equal()
  46. {
  47. table = new HashNode<Key, Value>*[_size];
  48. for (unsigned i = 0; i < _size; i++)
  49. table[i] = NULL;
  50. }
  51. template <class Key, class Value, class HashFunc, class EqualKey>
  52. HashMap<Key, Value, HashFunc, EqualKey>::~HashMap()
  53. {
  54. for (unsigned i = 0; i < _size; i++)
  55. {
  56. HashNode<Key, Value> *currentNode = table[i];
  57. while (currentNode)
  58. {
  59. HashNode<Key, Value> *temp = currentNode;
  60. currentNode = currentNode->next;
  61. delete temp;
  62. }
  63. }
  64. delete table;
  65. }
  66. template <class Key, class Value, class HashFunc, class EqualKey>
  67. bool HashMap<Key, Value, HashFunc, EqualKey>::insert(const Key& key, const Value& value)
  68. {
  69. int index = hash(key)%_size;
  70. HashNode<Key, Value> * node = new HashNode<Key, Value>(key,value);
  71. node->next = table[index];
  72. table[index] = node;
  73. return true;
  74. }
  75. template <class Key, class Value, class HashFunc, class EqualKey>
  76. bool HashMap<Key, Value, HashFunc, EqualKey>::del(const Key& key)
  77. {
  78. unsigned index = hash(key) % _size;
  79. HashNode<Key, Value> * node = table[index];
  80. HashNode<Key, Value> * prev = NULL;
  81. while (node)
  82. {
  83. if (node->_key == key)
  84. {
  85. if (prev == NULL)
  86. {
  87. table[index] = node->next;
  88. }
  89. else
  90. {
  91. prev->next = node->next;
  92. }
  93. delete node;
  94. return true;
  95. }
  96. prev = node;
  97. node = node->next;
  98. }
  99. return false;
  100. }
  101. template <class Key, class Value, class HashFunc, class EqualKey>
  102. Value& HashMap<Key, Value, HashFunc, EqualKey>::find(const Key& key)
  103. {
  104. unsigned index = hash(key) % _size;
  105. if (table[index] == NULL)
  106. return ValueNULL;
  107. else
  108. {
  109. HashNode<Key, Value> * node = table[index];
  110. while (node)
  111. {
  112. if (node->_key == key)
  113. return node->_value;
  114. node = node->next;
  115. }
  116. }
  117. }
  118. template <class Key, class Value, class HashFunc, class EqualKey>
  119. Value& HashMap<Key, Value, HashFunc, EqualKey>::operator [](const Key& key)
  120. {
  121. return find(key);
  122. }
  123. #endif

 

测试代码

  1. //首先要定义hash函数与比较函数
  2. class HashFunc
  3. {
  4. public:
  5. int operator()(const string & key )
  6. {
  7. int hash = 0;
  8. for(int i = 0; i < key.length(); ++i)
  9. {
  10. hash = hash << 7 ^ key[i];
  11. }
  12. return (hash & 0x7FFFFFFF);
  13. }
  14. };
  15. class EqualKey
  16. {
  17. public:
  18. bool operator()(const string & A, const string & B)
  19. {
  20. if (A.compare(B) == 0)
  21. return true;
  22. else
  23. return false;
  24. }
  25. };
  26. 测试用例
  27. int main()
  28. {
  29. HashMap<string, string, HashFunc, EqualKey> hashmap(100);
  30. hashmap.insert("hello", "world");
  31. hashmap.insert("why", "dream");
  32. hashmap.insert("c++", "good");
  33. hashmap.insert("welcome", "haha");
  34. cout << "after insert:" << endl;
  35. cout << hashmap.find("welcome").c_str() << endl;
  36. cout << hashmap.find("c++").c_str() << endl;
  37. cout << hashmap["why"].c_str() << endl;
  38. cout << hashmap["hello"].c_str() << endl;
  39. if (hashmap.del("hello"))
  40. cout << "remove is ok" << endl; //remove is ok
  41. cout << hashmap.find("hello").c_str() << endl; //not exist print NULL
  42. hashmap["why"] = "love";
  43. cout << hashmap["why"].c_str() << endl;
  44. return 0;
  45. }

 

转载:http://www.cnblogs.com/myd620/p/6349552.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/227354
推荐阅读
相关标签
  

闽ICP备14008679号