HashMap的C++实现
#include <iostream>
#include <cstring>
#include <string>
typedef unsigned int SIZE_T;
using namespace std; /** HashMap模板的C++实现,用拉链法解决冲突
*注意:需要为每一种KeyType两个仿函数:HashFunc and EqualKey
*/ template<typename KeyType, typename ValueType, typename HashFunc, typename EqualKey>
class HashMap{
#define DEFAULT_CAPACITY 43 // hash表初始化容量
#define LOADFACTOR 0.7 //负载因子 class KeyNode{ //存放key value对
public:
KeyType key;
ValueType value;
KeyNode *next;
KeyNode(){}
KeyNode(const KeyNode & rhs){
Assign(rhs);
}
const KeyNode & operator =(const KeyNode & rhs){
Assign(rhs);
return *this;
}
void Assign(const KeyNode & rhs){
this->key = rhs.key;
this->value = rhs.value;
this->next = rhs.next;
}
};
public:
HashMap(){
table = new KeyNode* [DEFAULT_CAPACITY];
::memset(table,,DEFAULT_CAPACITY * sizeof(KeyNode*));
size =;
capacity = DEFAULT_CAPACITY;
hash = HashFunc();
equal = EqualKey();
}
bool put(const KeyType & key, const ValueType & value){
SIZE_T index = hash(key) % capacity;
if(table[index] == NULL){
KeyNode *temp = new KeyNode();
temp->key = key;
temp->value = value;
temp->next =NULL;
table[index] = temp; }else{
KeyNode * pre=table[index]; while(pre->next != NULL){
if(equal(pre->key, key)) return false; //重复
pre = pre->next;
}
if(equal(pre->key, key)) return false; //重复
KeyNode *temp = new KeyNode();
temp->key = key;
temp->value = value;
temp->next =NULL;
pre->next = temp;
}
size ++;
if(size*1.0/capacity > LOADFACTOR) this->rehash();
return true;
}
bool remove(const KeyType & key){
SIZE_T index = hash(key) % capacity;
if(table[index] == NULL) return false;
KeyNode *temp = table[index];
if(equal(temp->key,key)){
table[index] = temp->next;
delete temp;
size --;
return true;
}
while(temp->next != NULL){
if(equal(temp->next->key, key)){
KeyNode * del = temp->next;
temp->next = del->next;
delete del;
size --;
return true;
}else{
temp = temp->next;
}
}
return false;
}
bool exist(const KeyType & key)const{
SIZE_T index = hash(key) % capacity;
if(table[index] == NULL) return false;
KeyNode *temp = table[index];
while(temp!= NULL){
if(equal(temp->key,key)) return true;
temp = temp->next;
}
return false;
}
/*
KeyNode find(const KeyType & key){
SIZE_T index = hash(key) % capacity;
if(table[index] == NULL) return NULL;
KeyNode *temp = table[index];
while(temp!= NULL){
if(equal(temp->key,key)) return *temp;
temp = temp->next;
}
return NULL;
}
*/
ValueType & operator[](const KeyType & key){
if(!exist(key)) put(key,ValueType());
return get(key);
}
SIZE_T Size(){
return size;
}
void display(){
cout << "size:"<<size<<endl;
for(SIZE_T i=;i<this->capacity;i++){
KeyNode * temp = table[i];
while(temp != NULL){
cout <<"("<<temp->key<<","<<temp->value<<") ";
temp = temp->next;
}
if(table[i]!=NULL) cout << endl;
}
}
~HashMap(){
this->destroy(table);
} private:
KeyNode ** table;
SIZE_T capacity;
SIZE_T size;
HashFunc hash;
EqualKey equal;
KeyNode ERROE; ValueType & get(const KeyType key){
SIZE_T index = hash(key) % capacity;
if(table[index] == NULL) return ERROE.value;
KeyNode *temp = table[index];
while(temp!= NULL){
if(equal(temp->key,key)) return temp->value;
temp = temp->next;
}
return ERROE.value;
}
//未实现
void rehash(){
//得到一个两倍左右大小的capacity(最好为素数),重新散列
}
void destroy(KeyNode ** hashtable){
for(SIZE_T i=;i<this->capacity;i++){
KeyNode * temp = hashtable[i];
while(temp != NULL){
KeyNode *del = temp;
temp = temp->next;
delete del;
}
}
delete []hashtable;
}
}; class HashFuncInt{
public:
SIZE_T operator()( int key)const{
return key;
}
}; class EqualFuncInt{
public:
bool operator()(int keyA, int keyB)const{
return keyA == keyB;
}
}; int main()
{
const int scale = ;
//内存泄露性能测试
while(false){
HashMap<int,int,HashFuncInt,EqualFuncInt> hm;
for(int i=;i<scale;i++){
hm.put(i,i*(i+));
}
hm.display();
for(int i=;i<scale;i+=)
hm.remove(i);
hm.display();
for(int i=;i<scale;i+=)
hm[i]=i*;
hm.display();
} //模板实用性测试 return ;
}
参考文献:
- http://blog.csdn.net/anialy/article/details/7620469
- http://blog.csdn.net/luxiaoxun/article/details/7786073
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()方法来将值划分到不同的桶 ...
随机推荐
- 【Java】 hashcode()和System.identityHashCode()
hashcode()和System.identityHashCode() openjdk8: http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/5b86f ...
- Python使用selenium进行爬虫(一)
JAVA爬虫框架很多,类似JSOUP,WEBLOGIC之类的爬虫框架都十分好用,个人认为爬虫的大致思路就是: 1.挑选需求爬的URL地址,将其放入需求网络爬虫的队列,也可以把爬到的符合一定需求的地址放 ...
- <Android 应用 之路> 天气预报(三)
昨天介绍了基本的载入界面,今天介绍下天气信息显示界面的代码 基本ListView显示 搜索框,查询城市 上一篇文章中,载入界面通过showWeatherInfo()方法跳转到天气信息显示界面 priv ...
- SharePoint 2010 缺少站点保存为模板选项
如果您尝试在SharePoint Server 2010中保存网站,并且没有看到"将网站另存为模板"选项,则可能是因为该网站已启用发布功能.如果使用站点发布功能,则有几个选项可将网 ...
- win10中使用win7/win8.1"个性化"
直接下载使用: 点此下载 设置 Windows Registry Editor Version 5.00 ; ; Created by http://winaero.com, reedited by ...
- 卓越管理的实践技巧(2)成功的委派任务 Setup for Successful Delegation
Setup for Successful Delegation 前文卓越管理的秘密(Behind Closed Doors)最后一部分提到了总结的13条卓越管理的实践技巧并列出了所有实践技巧名称的索引 ...
- [学习总结] python语言学习总结 (一)
还是不多说话了.. 1.eval函数 用法:eval(expression, globals=None, locals=None) 解释:将字符串str当成有效的表达式来求值并返回计算结果. 就是可以 ...
- 如何使用TensorFlow Hub和代码示例
任何深度学习框架,为了获得成功,必须提供一系列最先进的模型,以及在流行和广泛接受的数据集上训练的权重,即与训练模型. TensorFlow现在已经提出了一个更好的框架,称为TensorFlow Hub ...
- 如何使用动画库animate.css
animate.css是一个CSS3动画库,里面预设了抖动(shake).闪烁(flash).弹跳(bounce).翻转(flip).旋转(rotateIn/rotateOut).淡入淡出(fadeI ...
- java面试基础篇(一)
最近想深入的理解一下java 的工作机制,也是便于后期的面试. 1.A:HashMap和Hashtable有什么区别? Q:HashMap和Hashtable都实现了Map接口,因此很多特性非常相似. ...