STL的hasp_map學習筆記

來源:互聯網
上載者:User
 1.HASH函數的聲明問題

template <class _Key, class _Tp, class _HashFcn = hash<_Key>,

class _EqualKey = equal_to<_Key>,
class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
class hash_map
{
...
}

也就是說,在上例中,有以下等同關係:

...
hash_map<int, string> mymap;
//等同於:
hash_map<int, string, hash<int>, equal_to<int> > mymap
-------------------------------------------
1.有關hash函數的筆記
struct hash<int> {
size_t operator()(int __x) const { return __x; }
};
原來是個函數對象。在SGI STL中,提供了以下hash函數: 
struct hash<char*>
struct hash<const char*>
struct hash<char>
struct hash<unsigned char>
struct hash<signed char>
struct hash<short>
struct hash<unsigned short>
struct hash<int>
struct hash<unsigned int>
struct hash<long>
struct hash<unsigned long>
 
我的觀點:上面的內容還是比較簡單的,接著下面的內容非常關鍵
也就是說,如果你的key使用的是以上類型中的一種,你都可以使用預設的hash函數。當然你自己也可以定義自己的hash函數。對於自訂變數,你只能如此,例如對於string,就必須自訂hash函數。例如: 
struct str_hash{
size_t operator()(const string& str) const
{
unsigned long __h = 0;
for (size_t i = 0 ; i < str.size() ; i ++)
__h = 5*__h + str[i];
return size_t(__h);
}
};
//如果你希望利用系統定義的字串hash函數,你可以這樣寫:
struct str_hash{
size_t operator()(const string& str) const
{
return return __stl_hash_string(str.c_str());
}
};
在聲明自己的雜湊函數時要注意以下幾點: 
  1. 使用struct,然後重載operator().
  2. 返回是size_t
  3. 參數是你要hash的key的類型。
  4. 函數是const類型的。

 

直接替換成下面的聲明即可:

map<string, string> namemap; 
//改為:
hash_map<string, string, str_hash> namemap
2.有關比較函數的筆記
在map中的比較函數,需要提供less函數。如果沒有提供,預設的也是less< Key> 。在hash_map中,要比較桶內的資料和key是否相等,因此需要的是是否等於的函數:equal_to< Key> 。先看看equal_to的源碼: 
//本代碼可以從SGI STL
//先看看binary_function 函式宣告,其實只是定義一些類型而已。
template <class _Arg1, class _Arg2, class _Result>
struct binary_function {
typedef _Arg1 first_argument_type;
typedef _Arg2 second_argument_type;
typedef _Result result_type;
};
//看看equal_to的定義:
template <class _Tp>
struct equal_to : public binary_function<_Tp,_Tp,bool>
{
bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
};
如果你使用一個自訂的資料類型,如struct mystruct, 或者const char* 的字串,如何使用比較函數?使用比較函數,有兩種方法. 第一種是:重載==操作符,利用equal_to;看看下面的例子: 
struct mystruct{
int iID;
int len;
bool operator==(const mystruct & my) const{
return (iID==my.iID) && (len==my.len) ;
}
};
這樣,就可以使用equal_to< mystruct>作為比較函數了。另一種方法就是使用函數對象。自訂一個比較函數體: 
struct compare_str{
bool operator()(const char* p1, const char*p2) const{
return strcmp(p1,p2)==0;
}
};
有了compare_str,就可以使用hash_map了。 
typedef hash_map<const char*, string, hash<const char*>, compare_str> StrIntMap;
StrIntMap namemap;

------------------
#include   <iostream>  
  #include   <string>  
  #include   <ext/hash_map>  
  using   namespace   std;  
  using   namespace   __gnu_cxx;  
   
  namespace   __gnu_cxx  
  {  
          template<>   struct   hash<const   string>  
          {  
                  size_t   operator()(const   string&   s)   const  
                  {   return   hash<const   char*>()(   s.c_str()   );   }   //   __stl_hash_string  
          };  
          template<>   struct   hash<string>  
          {  
                  size_t   operator()(const   string&   s)   const  
                  {   return   hash<const   char*>()(   s.c_str()   );   }  
          };  
  }  
   
  int   main(   void   )  
  {  
          hash_map<string,int>   test;  
          test["abc"]   =   1;  
          cout   <<   test["abc"]   <<   endl;  
          system(   "pause"   );  
  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.