hash_set与hash_map

  1 // hash_set
  2 
  3 #include<iostream>
  4 #include<hash_set>
  5 
  6 using namespace std;
  7 
  8 void main()
  9 {
 10     hash_set<int> myset;// 不会排序  存入哈希表
 11     myset.insert(123);
 12     myset.insert(1123);
 13     myset.insert(23);
 14 
 15     for (auto i : myset )
 16     {
 17         cout << i << endl;
 18     }
 19 
 20     for(auto ib = myset.begin(),ie = myset.end();ib!=ie;ib++)
 21     {
 22         cout << *ib << endl;
 23     }
 24 
 25     auto i = myset.find(23);
 26     if (i != myset.end())
 27     {
 28         cout << "find = " << *i << endl;
 29     }
 30 
 31     cin.get();
 32 }
 33 
 34 //------------------------------------------------------------------------
 35 
 36 struct strequ
 37 {
 38     bool operator ()(const char *str1,const char *str2) const
 39     {
 40         return (strcmp(str1,str2) == 0);    
 41     }
 42 };
 43 
 44 void main()
 45 {
 46     hash_set<char*,hash_compare<char*>,strequ> myhash;
 47 
 48     myhash.insert("abc");
 49     myhash.insert("zbc");
 50     myhash.insert("cbc");
 51     myhash.insert("bbc");
 52 
 53     auto i = myhash.find("cbc");
 54     if (i != myhash.end())
 55     {
 56         cout << "find = " << *i << endl;
 57     }
 58 
 59 
 60 
 61     cin.get();
 62 }
 63 
 64 //------------------------------------------------------------------------
 65 
 66 // hash_map
 67 
 68 
 69 #include<iostream>
 70 #include<hash_map>
 71 
 72 using namespace std;
 73 
 74 
 75 void main()
 76 {
 77     hash_map<char*,int> mymap;
 78     mymap["师长"] = 8;
 79     mymap["旅长"] = 7;
 80     mymap["团长"] = 6;
 81     mymap["司令"] = 10;
 82     mymap["军长"] = 9;
 83     mymap["军长"] = 9;
 84     mymap["军长"] = 19;// 以军长为准 只有一个
 85     
 86     for (auto i : mymap)
 87     {
 88         cout << i.first << "  " << i.second << endl;
 89     }
 90     
 91 
 92     for (auto ib=mymap.begin().ie=mymap.end();ib!=ie;ib++ )
 93     {
 94         cout << (*ib).first << " " << (*ib).second << endl;
 95     }
 96     
 97 
 98     auto it = mymap.find("师长");
 99     if (it != mymap.end())
100     {
101         cout << (*it).first << " " << (*it).second << endl;
102     }
103 
104 
105     cin.get();
106 }

 

posted on 2015-06-14 18:04  Dragon-wuxl  阅读(142)  评论(0)    收藏  举报

导航