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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

数据结构-哈希与映射

發(fā)布時(shí)間:2025/3/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构-哈希与映射 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

二叉樹(shù)映射map

#include<iostream> #include<map> //映射(也叫字典),二叉樹(shù)映射,不是哈希映射 using namespace std; int main() {cout << "二叉樹(shù)(紅黑樹(shù))" << endl;map<string, int> m;m["bill"]= 67;m["toney"] = 98;m["Mary"] = 100;cout << m["Mary"] << endl;system("pause");return 0; }

數(shù)組優(yōu)點(diǎn)?

#include<iostream> #include<string> using namespace std; int main() {//數(shù)組的優(yōu)點(diǎn)//對(duì)下標(biāo)(索引)進(jìn)行非常快的地址運(yùn)算//取arr[0] arr[23232] 速度一樣int arr[100000];int length = sizeof(arr) / sizeof(int);cout << length << endl;for (int i = 0; i < length; i++){arr[i] = i%100;}cout << arr[8] << endl;cout << arr[18] << endl;cout << arr[98756] << endl;system("pause");return 0; }

線性映射?

LinearMap.h

#ifndef LINEARMAP_H #define LINEARMAP_H #include<vector> template<class Key,class Value> class LinearMap //線性映射-沒(méi)用的東西 { public:LinearMap(int size = 101) :arr(size){currentSize = 0;}void Put(const Key &k, const Value &v){arr[currentSize] = DataEntry(k, v);++currentSize;}Value Get(const Key & k){//線性查找for (size_t i = 0; i < currentSize; ++i){if (arr[i].key == k){return arr[i].value;}}return Value();} private:struct DataEntry {Key key;Value value;DataEntry(const Key & k = Key(),const Value & v = Value()) : key(k), value(v) {}};std::vector<DataEntry> arr;int currentSize; };#endif

linearMap.cpp

#include<iostream> #include<string> #include"LinearMap.h" using namespace std; int main() {//沒(méi)用的,僅學(xué)習(xí)的線性映射LinearMap<string, int> lm;lm.Put("bill", 99);lm.Put("Tom", 88);cout << "Lin earMap:" << lm.Get("Tom") << endl;system("pause");return 0; }

運(yùn)行結(jié)果:

哈希映射

HashMap.h

#ifndef LINEARMAP_H #define LINEARMAP_H #include<vector> template<class Key, class Value> class HashMap //哈希映射 最快 { public:HashMap(int size = 101) :arr(size){currentSize = 0;}void Put(const Key &k, const Value &v){int pos = myHash(k);arr[pos] = DataEntry(k, v);++currentSize;}Value Get(const Key & k){int pos = myHash(k);if (arr[pos].key == k)return arr[pos].value;elsereturn Value();}unsigned hash(const Key & k) const{unsigned int hashVal = 0;const char* keyp = reinterpret_cast<const char*>(&k); //轉(zhuǎn)型for (size_t i = 0; i < sizeof(Key); i++){hashVal = 37 * hashVal + keyp[i];}return hashVal;}int myHash(const Key & k) const{unsigned hashVal = hash(k);hashVal %= arr.size();return hashVal;}private:struct DataEntry {Key key;Value value;DataEntry(const Key & k = Key(),const Value & v = Value()) : key(k), value(v) {}};std::vector<DataEntry> arr;int currentSize; };#endif

HashMap.cpp

#include<iostream> #include<string> #include"HashMap.h" using namespace std; int main() {HashMap<string, int> hm;/*cout << hm.hash("Bill") << endl;cout << hm.myHash("Bill") << endl;*/hm.Put("Bill", 33);hm.Put("Tom", 98);cout << hm.Get("Bill") << endl;//cout << hm.Get("Tom") << endl; 取兩個(gè)就會(huì)錯(cuò)誤system("pause");return 0; }

運(yùn)行結(jié)果:

c++自帶的哈希映射(STL)

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS #include<iostream> #include<string> #include<hash_map> using namespace std; //使用C++封裝好的hash_map //又要排序又要映射時(shí)就用tree_map(二叉樹(shù)映射) int main() {hash_map<string, int> hm;hm["Bill"] = 12;hm["Tom"] = 22;hm["Mary"] = 34;cout << hm["Mary"] << endl;cout << hm["Tom"] << endl;system("pause");return 0; }

運(yùn)行結(jié)果:

?

?

?

?

?

?

總結(jié)

以上是生活随笔為你收集整理的数据结构-哈希与映射的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。