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

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

头文件如下hashmap.h

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

测试代码

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

c++ 实现hashmap的更多相关文章

  1. HashMap与TreeMap源码分析

    1. 引言     在红黑树--算法导论(15)中学习了红黑树的原理.本来打算自己来试着实现一下,然而在看了JDK(1.8.0)TreeMap的源码后恍然发现原来它就是利用红黑树实现的(很惭愧学了Ja ...

  2. HashMap的工作原理

    HashMap的工作原理   HashMap的工作原理是近年来常见的Java面试题.几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道HashTable和HashMap之间 ...

  3. 计算机程序的思维逻辑 (40) - 剖析HashMap

    前面两节介绍了ArrayList和LinkedList,它们的一个共同特点是,查找元素的效率都比较低,都需要逐个进行比较,本节介绍HashMap,它的查找效率则要高的多,HashMap是什么?怎么用? ...

  4. Java集合专题总结(1):HashMap 和 HashTable 源码学习和面试总结

    2017年的秋招彻底结束了,感觉Java上面的最常见的集合相关的问题就是hash--系列和一些常用并发集合和队列,堆等结合算法一起考察,不完全统计,本人经历:先后百度.唯品会.58同城.新浪微博.趣分 ...

  5. 学习Redis你必须了解的数据结构——HashMap实现

    本文版权归博客园和作者吴双本人共同所有,转载和爬虫请注明原文链接博客园蜗牛 cnblogs.com\tdws . 首先提供一种获取hashCode的方法,是一种比较受欢迎的方式,该方法参照了一位园友的 ...

  6. HashMap与HashTable的区别

    HashMap和HashSet的区别是Java面试中最常被问到的问题.如果没有涉及到Collection框架以及多线程的面试,可以说是不完整.而Collection框架的问题不涉及到HashSet和H ...

  7. JDK1.8 HashMap 源码分析

    一.概述 以键值对的形式存储,是基于Map接口的实现,可以接收null的键值,不保证有序(比如插入顺序),存储着Entry(hash, key, value, next)对象. 二.示例 public ...

  8. HashMap 源码解析

    HashMap简介: HashMap在日常的开发中应用的非常之广泛,它是基于Hash表,实现了Map接口,以键值对(key-value)形式进行数据存储,HashMap在数据结构上使用的是数组+链表. ...

  9. java面试题——HashMap和Hashtable 的区别

    一.HashMap 和Hashtable 的区别 我们先看2个类的定义 public class Hashtable extends Dictionary implements Map, Clonea ...

  10. 再谈HashMap

    HashMap是一个高效通用的数据结构,它在每一个Java程序中都随处可见.先来介绍些基础知识.你可能也知 道,HashMap使用key的hashCode()和equals()方法来将值划分到不同的桶 ...

随机推荐

  1. IDEA访问不到SpringBoot项目webapp下的内容

    参考:https://blog.csdn.net/weixin_43105335/article/details/87863847 https://blog.csdn.net/qq_34797335/ ...

  2. Shell脚本- 单条命令循环执行重复工作

    关于shell for循环具体详细说明可参考:http://wiki.jikexueyuan.com/project/linux-command/chap34.html example: 分别在com ...

  3. Hbase的基本操作(CDH组件可用)

    Habse创建一张表:    1,创建一个命名空间NameSpace(命名空间NameSpace指的是一个表的逻辑分组 ,同一分组中的各个表有类似的用途,相当于关系型数据库中的DataBase)    ...

  4. 使用PHP实现手机端APP支付宝的支付功能

    最近应业务需求,做了支付宝支付和微信支付,今天分享一下手机端app支付宝支付对接流程,实际开发过程是前后端分离,前端调用后端API接口,实现功能返回数据,我所用的跨挤啊为TP5,大致可以分为四步: 1 ...

  5. Java Swing实现展示数据,以及过滤排序

    public class RelationCostctrTable extends DefaultTableModel { public RelationCostctrTable(Vector< ...

  6. nginx 配置文件学习

    ######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_ ...

  7. OO第二单元单元总结

    总述 OO的第二单元主题是电梯调度,与第一单元注重对数据的输入输出的处理.性能的优化不同,第二单元的重心更多的是在线程安全与线程通信上.这此次单元实验之前,我并未对线程有过了解,更谈不上“使用经验”, ...

  8. Intellij IDEA环境配置RestEasy,SpringMVC+RestEasy

    在SpringMvc中配置RestEasy,需要以下步骤 1.通过maven导入restEasy所需要的jar包 2.在web.xml文件中添加相应的配置. 3.编写服务. 具体步骤: 1.通过mav ...

  9. XBee® ZigBee 模块使用方法

    Digi的ZigBee模块简称S2,根据芯片版本的不同历史上分别有S2,S2B,S2C等,每次硬件平台升级,都会引入一个新的尾缀字母.历史版本中S2和S2B已经停产并被S2C替代.当前S2C是主流平台 ...

  10. SAS 操作数据集的观测

    SAS  操作数据集的观测 1. SAS表达式 表达式是操作数和操作符的序列,该序列会形成一组可执行并产生 结果值的指令.其中,操作数可以是常量.变量或表达式:操作符是表 示比较.数学计算或逻辑运算的 ...