数据结构-哈希与映射
生活随笔
收集整理的這篇文章主要介紹了
数据结构-哈希与映射
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
二叉樹映射map
#include<iostream> #include<map> //映射(也叫字典),二叉樹映射,不是哈希映射 using namespace std; int main() {cout << "二叉樹(紅黑樹)" << endl;map<string, int> m;m["bill"]= 67;m["toney"] = 98;m["Mary"] = 100;cout << m["Mary"] << endl;system("pause");return 0; }數組優點?
#include<iostream> #include<string> using namespace std; int main() {//數組的優點//對下標(索引)進行非常快的地址運算//取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 //線性映射-沒用的東西 { 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; };#endiflinearMap.cpp
#include<iostream> #include<string> #include"LinearMap.h" using namespace std; int main() {//沒用的,僅學習的線性映射LinearMap<string, int> lm;lm.Put("bill", 99);lm.Put("Tom", 88);cout << "Lin earMap:" << lm.Get("Tom") << endl;system("pause");return 0; }運行結果:
哈希映射
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); //轉型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; };#endifHashMap.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; 取兩個就會錯誤system("pause");return 0; }運行結果:
c++自帶的哈希映射(STL)
#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS #include<iostream> #include<string> #include<hash_map> using namespace std; //使用C++封裝好的hash_map //又要排序又要映射時就用tree_map(二叉樹映射) 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; }運行結果:
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的数据结构-哈希与映射的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白盒测试-条件覆盖-短路陷阱
- 下一篇: 制备pdms膜的方法_船体用钢板基底超疏