一天一点数据结构+算法:复习Hash Map

LinearMap就是普通的数组。

HashMap是自己做的哈希映射。

HashMap查找的时间复杂度为O(1),十分快。二叉查找树的查找速度是O(LogN),所以哈希在查找数据时表现很好。

但是,Hash并不适合做排序,而二叉查找树中和了查找和排序两个功能。

Hash_Map
 1 #include <vector>
 2 
 3 template <class Key, class Value>
 4 class HashMap //哈希映射
 5 
 6 {
 7 public:
 8     HashMap(int size = 101):arr(size){
 9     
10      currentSize = 0;
11     
12     }
13 
14     void Put(const Key & k, const Value & v)
15     {
16         int pos = myhash(k);
17         arr[pos] = DataEntry(k, v);
18         ++currentSize;        
19     }
20 
21     Value Get(const Key & k)
22     {
23         int pos = myhash(k);
24         if(arr[pos].key == k)
25             return arr[pos].value;
26         else
27             return Value();
28 
29     }
30 
31     unsigned hash(const Key & k) const
32     {
33     
34         unsigned int hashVal = 0;
35         const char *keyp = reinterpret_cast<const char *> (&k);
36         for(size_t i = 0; i<sizeof(Key); i++)
37             hashVal = 37 * hashVal + keyp[i];
38         return hashVal;
39 
40     }
41 
42     int myhash(const Key & k) const
43     {
44     
45       unsigned hashVal = hash(k);
46       hashVal %= arr.size();
47       return hashVal;
48     }
49 
50 
51 private:
52 
53     struct DataEntry{
54     
55         Key key;
56         Value value;
57 
58         DataEntry(const Key & k = Key(), const Value & v = Value()):
59         key(k),value(v){}
60     };
61 
62     std::vector<DataEntry> arr;
63 
64     int currentSize;
65     
66 };
LinearMap
 1 #include <vector>
 2 
 3 template <class Key, class Value>
 4 class LinearMap //线性运算
 5 
 6 {
 7 public:
 8     LinearMap(int size = 101):arr(size){
 9     
10      currentSize = 0;
11     
12     }
13 
14     void Put(const Key & k, const Value & v)
15     {
16     
17         arr[currentSize] = DataEntry(k,v);
18         ++currentSize;
19     
20     }
21 
22     Value Get(const Key & k)
23     {
24     
25         //线性查找
26         for(size_t i = 0; i<currentSize; ++i) 
27         {
28         
29             if(arr[i].key == k)
30                 return arr[i].value;
31         }
32     
33         return Value();
34     }
35 
36     
37 private:
38 
39     struct DataEntry{
40     
41         Key key;
42         Value value;
43 
44         DataEntry(const Key & k = Key(), const Value & v = Value()):
45         key(k),value(v){}
46     };
47 
48     std::vector<DataEntry> arr;
49 
50     int currentSize;
51     
52 };
main
 1 #include <iostream>
 2 #include <map>//二叉树映射,不是哈希映射
 3 #include <hash_map>  //C++自己做的hash,头文件里不是
 4 #include <string>
 5 #include "LinearMap.h"
 6 #include "HashMap.h"
 7 
 8 
 9 using namespace std;
10 
11 int main()
12 {
13     cout << "红黑树映射" << endl;
14     map<string,int> m;
15     m["Bill"] = 98;
16     m["Tom"] = 67;
17     m["Marry"] = 100;
18 
19     cout << m["Tom"] << endl;
20 
21     //数组的优点
22     int a[100000];
23     for(int i = 0; i< 100000; i++)
24         a[i] = i % 100;
25 
26 
27     cout << a[8] << endl;
28 
29     LinearMap<string, int> lm;
30     lm.Put("Bill", 99);
31     lm.Put("Tom", 88);
32     lm.Put("Marry", 77);
33     cout << "LinearMap: " << lm.Get("Tom") << endl;
34     
35     //My HashMap
36     cout << "MyHashMap: " << endl;
37     HashMap<string, int> myHMap;
38 
39     cout << myHMap.hash("Bill") << endl;
40 
41     myHMap.Put("Bill", 999);
42     myHMap.Put("Tom", 888);
43     myHMap.Put("Marry",777);
44     cout << myHMap.Get("Tom") << endl;
45 
46     cout << endl;
47 
48     cout << "微软做的哈希映射:" << endl;
49     hash_map<string, int> hm;
50     hm["Bill"] = 12;
51     hm["Tom"] = 22;
52 
53     cout << hm["Tom"] << endl;
54 
55 
56     return 0;
57 }

 

posted @ 2013-01-14 01:48  uniquews  阅读(430)  评论(0编辑  收藏  举报