日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

hashmap的C++实现

發布時間:2025/4/16 c/c++ 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hashmap的C++实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

hashmap的C++實現

按照hashmap的基本原理用C++實現了簡單的基本功能,復雜的實現參考C++庫的源碼,C++最新的標準庫里已經有以下四種基于hashtable的容器:

unordered_set?(C++11)?unordered_multiset?(C++11)?unordered_map?(C++11)?unordered_multimap?(C++11)。具體參考:http://en.cppreference.com/w/

/** HashMap.h* Author: luxiaoxun*/#ifndef HASHMAP_H_ #define HASHMAP_H_#include <iostream> using namespace std;//List all the integer number no less than 57 total number is 28 //And each number is about half of its next number static int prime[28] = {57, 97, 193, 389, 769,1543, 3079, 6151, 12289, 24593,49157, 98317, 196613, 393241, 786433,1572869, 3145739, 6291469, 12582917, 25165843,50331653, 100663319, 201326611, 402653189, 805306457,1610612741 };class HashMapUtil { public:static int find_NextPrimeNumber(int current){//Find the next prime number by searching in the prime number listint i = 0;for( ; i < 28 ; i++ )if(current < prime[i])break;return prime[i]; //return the next larger prime. } };template <class Key, class Value, class HashFunc, class EqualKey> class HashMap { private:template <class _Key, class _Value>class KeyNode{public:_Value value; //Store the value_Key key; //Store the keywordint used;//if the type of Value/Key is your own class, make sure they can handle copy constructor and operator =KeyNode():used(0){}KeyNode(const KeyNode & kn){value = kn.value;key = kn.key;used = kn.used;}KeyNode & operator=(const KeyNode & kn){if(this == &kn) return *this;value = kn.value;key = kn.key;used = kn.used;return *this;}};public:HashMap();~HashMap();bool insert(const Key& hashKey, const Value& value);bool remove(const Key& hashKey);void rehash(); //use it when rehashingValue& find(const Key& hashKey);const Value& operator [](const Key& hashKey) const;Value& operator [](const Key& hashKey);private:HashFunc hash;EqualKey equal;HashMapUtil hml;KeyNode<Key ,Value> *table;int size; //current number of itmesint capacity; //capacity of the arraystatic const double loadingFactor;int findKey(const Key& hashKey); //find the index of a key };template<class Key , class Value , class HashFunc , class EqualKey> const double HashMap<Key, Value, HashFunc, EqualKey>::loadingFactor = 0.9;template<class Key , class Value , class HashFunc , class EqualKey> HashMap<Key, Value, HashFunc, EqualKey>::HashMap() {hash = HashFunc();equal = EqualKey();hml = HashMapUtil();capacity = hml.find_NextPrimeNumber(0); //initialize the capacity with first primer 57//resize the table with capacity because an extra one is used//to return the NULL type of Value in the function findtable = new KeyNode<Key,Value>[capacity+1];for(int i = 0 ; i < capacity ; i++) //initialize the tabletable[i].used = 0;size = 0; }template<class Key, class Value, class HashFunc, class EqualKey> HashMap<Key, Value, HashFunc, EqualKey>::~HashMap() {delete []table; }template<class Key, class Value, class HashFunc, class EqualKey> bool HashMap<Key, Value, HashFunc, EqualKey>::insert(const Key& hashKey, const Value& value) {int index = hash(hashKey)%capacity;//cout<<"Index is "<<index<<endl;if(table[index].used == 1) //the key-value's hash is unique {//cout<<"The key-value must be unique!"<<endl;return false;}table[index].used = 1; //modify the KeyNodetable[index].key = hashKey;table[index].value = value;size++;//if the table's size is too large ,then rehash itif (size >= capacity * loadingFactor)rehash();return true; }template<class Key, class Value, class HashFunc, class EqualKey> void HashMap<Key, Value, HashFunc, EqualKey>::rehash() {int pastsize = capacity;//create a new array to copy the information in the old tablecapacity = hml.find_NextPrimeNumber(capacity);KeyNode<Key,Value>* tmp = new KeyNode<Key,Value>[capacity];for(int i = 0 ; i < pastsize ; i++){if(table[i].used == 1) //copy the KeyNode into the tmp array {tmp[i] = table[i];}}delete []table; //release the memory of the old table table = new KeyNode<Key,Value>[capacity+1]; //resize the tablefor(int i = 0 ; i < capacity ; i++) //initialize the table {table[i].used = 0;}for(int i = 0 ; i < pastsize ; i++) //insert the item into the table one by one {if(tmp[i].used == 1)insert(tmp[i].key, tmp[i].value);}delete []tmp; //delete the tmp array }template<class Key, class Value, class HashFunc, class EqualKey> bool HashMap<Key, Value, HashFunc, EqualKey>::remove(const Key& hashKey) {int index = findKey(hashKey); //find the index of the keyif(index < 0) //if find modify the flag with 0,else print out "no such key!" {cout<<"No such Key!"<<endl;return false;}else{table[index].used = 0;size--;return true;} }template<class Key, class Value, class HashFunc, class EqualKey> Value& HashMap<Key, Value, HashFunc, EqualKey>::find(const Key& hashKey) {int index = findKey(hashKey);if(index < 0) //if index <0 ,not found,else return the index {cout<<"can not find the key!"<<endl;return table[capacity].value; //return NULL }else{return table[index].value;} }template<class Key, class Value, class HashFunc, class EqualKey> const Value& HashMap<Key, Value, HashFunc, EqualKey>::operator[](const Key& hashKey) const {return find(hashKey); //overload the operation to return the value of the element }template<class Key, class Value, class HashFunc, class EqualKey> Value& HashMap<Key, Value, HashFunc, EqualKey>::operator[](const Key& hashKey) {return find(hashKey); //overload the operation to return the value of the element }template<class Key, class Value, class HashFunc, class EqualKey> int HashMap<Key, Value, HashFunc, EqualKey>::findKey(const Key& hashKey) {int index = hash(hashKey)%capacity;if ((table[index].used != 1) || !equal(table[index].key,hashKey))return -1;elsereturn index; }#endif /* HASHMAP_H_ */

這里實現的key必須是unique的,否則就要處理沖突的問題,可以通過[]操作修改對應的value,但是不能通過[]添加value,標準庫里的是可以的。

C++編譯器不支持模板頭文件和實現代碼分離的編譯,如果的類實現和類聲明分別放在cpp文件和h頭文件里,那么在測試代碼里include要加上實現的代碼,比如加上#include"HashMap.cpp"。使用hashmap,需要自己提供針對key的hash函數對象和equal函數對象,測試代碼:

#include "HashMap.h" #include <string> #include <iostream> using namespace std;//Hash function you provided must be correspond to the type of the Key class HashFunc { public:int operator()(const string & key ){int hash = 0;for(int i = 0; i < key.length(); ++i){hash = hash << 7 ^ key[i];}return (hash & 0x7FFFFFFF);} };//Equal function you provided to check whether two Keys are equal //must be correspond to the type of the Key class EqualKey { public:bool operator()(const string & A ,const string & B){if(A.compare(B) == 0)return true; //if equal return trueelsereturn false; //else false } };int main() {HashMap<string,string,HashFunc,EqualKey> hm;hm.insert("hello" , "you");hm.insert("why" , "dream");hm.insert("java" ,"good");hm.insert("welcome" ,"haha");hm.insert("welcome" ,"hehe"); //error, key-value must be unique cout<<"after insert:"<<endl;cout<<hm.find("welcome")<<endl;cout<<hm.find("java")<<endl;cout<<hm["why"]<<endl;cout<<hm["hello"]<<endl;if(hm.remove("hello"))cout<<"remove is ok"<<endl; //remove is okcout<<hm.find("hello")<<endl; //not exist print NULL hm["why"] = "love"; //modify the value cout<<hm["why"]<<endl;return 0; } ? ? 本文轉自阿凡盧博客園博客,原文鏈接:http://www.cnblogs.com/luxiaoxun/archive/2012/09/02/2667782.html,如需轉載請自行聯系原作者
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的hashmap的C++实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 青青在线视频观看 | 91爱爱网| 亚洲国产精品欧美久久 | 免费动漫av| 青青超碰 | 中文字幕人妻一区二区三区 | 亚洲性图av | 无人在线观看的免费高清视频 | 亚洲成人无码久久 | 国产成人精品二区三区亚瑟 | 成人av高清| 老男人av| 红桃视频在线播放 | 久久黄色免费网站 | 国产成人精品亚洲线观看 | 天堂资源在线 | 一区三区视频 | 国产噜噜噜噜噜久久久久久久久 | 无码一区二区三区免费视频 | 天天爽天天干 | 欧美一级黄视频 | 九色免费视频 | 国产中年熟女高潮大集合 | 在线视频 一区二区 | 台湾少妇xxxx做受 | 诱人的乳峰奶水hd | 在线观看免费观看在线 | 97超碰中文字幕 | 亚洲精品7777 | av在线小说| 97av在线播放 | 午夜av导航 | 国产精品二区在线观看 | 亚洲国产精品视频 | 美女网站免费观看 | 国产成人一区 | 2022精品国偷自产免费观看 | 住在隔壁的她动漫免费观看全集下载 | 亚洲欧洲免费 | 国产精品激情 | 色哟哟在线观看视频 | 日韩精品av一区二区三区 | 欧美精品一区二区三区视频 | 黄色美女免费网站 | 香港三级在线视频 | 调教一区二区 | 日本wwwxxx| www.日日操| 欧美日韩精品综合 | 亚洲乱熟女一区二区三区小说 | 制服丝袜国产精品 | 毛片高清 | 波多野久久 | 欧美日韩一区在线播放 | 日本加勒比一区 | 午夜av一区二区三区 | 欧美一区2区 | 欧美亚洲精品在线 | 深夜福利视频网站 | 国产精品亚洲AV色欲三区不卡 | 亚洲一区二区在线播放 | 久久久18禁一区二区三区精品 | 秋霞av一区二区三区 | 精品国产第一页 | 日韩极品在线观看 | 久久一区二区电影 | аⅴ资源新版在线天堂 | 久久动态图 | 成人久久精品人妻一区二区三区 | 国产精品h| 国产伦精品一区二区三区视频我 | 久久在线精品 | 日韩日b| 国产精品伦理 | 欧美三级三级三级爽爽爽 | 182tv午夜福利在线观看 | 黄色免费在线观看视频 | 国产大奶在线观看 | 精品一区二区三区蜜桃 | 国产极品一区二区 | 骚虎av| 二级毛片| 亚洲在线一区 | 亚洲第一免费 | 欧美性猛交xxx乱久交 | 国产精品香蕉 | 亚洲另类色综合网站 | 欧美日韩综合视频 | 麻豆久久久久久久 | 国产suv精品一区二区883 | 无码熟妇人妻av | 天堂8在线 | 国产人与zoxxxx另类 | 手机在线看片国产 | 久久人妻无码aⅴ毛片a片app | av在线片| 男人和女人日批视频 | 激情欧美综合 | 69精品无码成人久久久久久 |