hashmap C++实现
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 std::string& key )
{
int hash = ;
for(int i = ; i < key.length(); ++i)
{
hash = hash << ^ key[i];
}
return (hash & 0x7FFFFFFF);
}
}; class EqualKey
{
public:
bool operator()(const std::string& A, const std::string& B)
{
if (A.compare(B) == )
return true;
else
return false;
}
}; //测试用例
int main()
{
HashMap<std::string, std::string, HashFunc, EqualKey> hashmap(); 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 ;
}
原作者:https://www.cnblogs.com/myd620/p/6349552.html
hashmap C++实现的更多相关文章
- HashMap与TreeMap源码分析
1. 引言 在红黑树--算法导论(15)中学习了红黑树的原理.本来打算自己来试着实现一下,然而在看了JDK(1.8.0)TreeMap的源码后恍然发现原来它就是利用红黑树实现的(很惭愧学了Ja ...
- HashMap的工作原理
HashMap的工作原理 HashMap的工作原理是近年来常见的Java面试题.几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道HashTable和HashMap之间 ...
- 计算机程序的思维逻辑 (40) - 剖析HashMap
前面两节介绍了ArrayList和LinkedList,它们的一个共同特点是,查找元素的效率都比较低,都需要逐个进行比较,本节介绍HashMap,它的查找效率则要高的多,HashMap是什么?怎么用? ...
- Java集合专题总结(1):HashMap 和 HashTable 源码学习和面试总结
2017年的秋招彻底结束了,感觉Java上面的最常见的集合相关的问题就是hash--系列和一些常用并发集合和队列,堆等结合算法一起考察,不完全统计,本人经历:先后百度.唯品会.58同城.新浪微博.趣分 ...
- 学习Redis你必须了解的数据结构——HashMap实现
本文版权归博客园和作者吴双本人共同所有,转载和爬虫请注明原文链接博客园蜗牛 cnblogs.com\tdws . 首先提供一种获取hashCode的方法,是一种比较受欢迎的方式,该方法参照了一位园友的 ...
- HashMap与HashTable的区别
HashMap和HashSet的区别是Java面试中最常被问到的问题.如果没有涉及到Collection框架以及多线程的面试,可以说是不完整.而Collection框架的问题不涉及到HashSet和H ...
- JDK1.8 HashMap 源码分析
一.概述 以键值对的形式存储,是基于Map接口的实现,可以接收null的键值,不保证有序(比如插入顺序),存储着Entry(hash, key, value, next)对象. 二.示例 public ...
- HashMap 源码解析
HashMap简介: HashMap在日常的开发中应用的非常之广泛,它是基于Hash表,实现了Map接口,以键值对(key-value)形式进行数据存储,HashMap在数据结构上使用的是数组+链表. ...
- java面试题——HashMap和Hashtable 的区别
一.HashMap 和Hashtable 的区别 我们先看2个类的定义 public class Hashtable extends Dictionary implements Map, Clonea ...
- 再谈HashMap
HashMap是一个高效通用的数据结构,它在每一个Java程序中都随处可见.先来介绍些基础知识.你可能也知 道,HashMap使用key的hashCode()和equals()方法来将值划分到不同的桶 ...
随机推荐
- perl 数组变量(Array) 转载
Perl 变量(2)--数组 原文地址:Perl 变量(2)--数组 作者:飞鸿无痕 二.数组 数组是标量数据的有序列表. 数组可以含任意多个元素.最小的数组可以不含元素,而最大的数组可以占满全部可用 ...
- Java进阶知识06 Hibernate一对一单向外键关联(Annotation+XML实现)
1.Annotation 注解版 1.1.创建Husband类和Wife类 package com.shore.model; import javax.persistence.Entity; impo ...
- MessagePack Java Jackson Dataformat - 安装
中文标题[MessagePack 的 Jackson 数据格式] 本页面中的所有示例程序,请到代码库 https://github.com/cwiki-us-demo/serialize-deseri ...
- poi 1017 Packets 贪心+模拟
Packets Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 48349 Accepted: 16392 Descrip ...
- 关于int main(int argc,char* argv[])详解
平时在VS的环境下,主函数总会看到这两个参数,今天突然很想知道这两个参数的原理以及作用,因此查了下资料.真心受教了. 下面的博文是在百度空间看一位大神的,原文链接:http://hi.baidu.co ...
- Linux下Weblogic 11g R1安装和配置
自己留着好好看看! 文章摘自 :http://blog.163.com/ng_0125@126/blog/static/170162360201311773235995/ 1. 安装前 1 确定w ...
- python3 输入与输出
pyhon3 io 输入和输出myread=open('E:/路径.txt')#open()会将返回一个file对象mywrite=open('E:/3/路径.txt','w')#后面w是如果文件存在 ...
- 关于一次同余方程的一类解法(exgcd,CRT,exCRT)
1.解同余方程: 同余方程可以转化为不定方程,其实就是,这样的问题一般用拓展欧几里德算法求解. LL exgcd(LL a,LL b,LL &x,LL &y){ if(!b){ x=; ...
- RESTful API是什么?
1. REST 是Repersentational State Transfer的缩写 翻译为"表述性状态传递",那么什么是表述性状态传递呢?为了理解这个词语,我们从"R ...
- TCP层recvmsg系统调用的实现分析
概述 recvmsg系统调用在tcp层的实现是tcp_recvmsg函数,该函数完成从接收队列中读取数据复制到用户空间的任务:函数在执行过程中会锁定控制块,避免软中断在tcp层的影响:函数会涉及从接收 ...