C++ STL中的雜湊表 hash_map

來源:互聯網
上載者:User

標籤:

在定義hash_map容器的時候,不僅需要指定鍵和值的類型,還需要指定hash函數和相等函數


(一)hash_map 的hash函數

hash< int>到底是什麼樣子?看看源碼:

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 __stl_hash_string(str.c_str());        }};

在聲明自己的雜湊函數時要注意以下幾點:

  1. 使用struct,然後重載operator().
  2. 返回是size_t
  3. 參數是你要hash的key的類型。
  4. 函數是const類型的。

(二)hash_map 的比較函數

在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;namemap["嶽不群"]="華山派掌門人,人稱君子劍";namemap["張三丰"]="武當掌門人,太極拳創始人";namemap["東方不敗"]="第一高手,葵花寶典";

(三)如何在hash_map中加入自己定義的類型?

#include <hash_map>#include <string>#include <iostream>using namespace std;//define the classclass ClassA{        public:        ClassA(int a):c_a(a){}        int getvalue()const { return c_a;}        void setvalue(int a){c_a;}        private:        int c_a;};//1 define the hash functionstruct hash_A{        size_t operator()(const class ClassA & A)const{                //  return  hash<int>(classA.getvalue());                return A.getvalue();        }};

//2 define the equal functionstruct equal_A{        bool operator()(const class ClassA & a1, const class ClassA & a2)const{                return  a1.getvalue() == a2.getvalue();        }};int main(){        hash_map<ClassA, string, hash_A, equal_A> hmap;        ClassA a1(12);        hmap[a1]="I am 12";        ClassA a2(198877);        hmap[a2]="I am 198877";                cout<<hmap[a1]<<endl;        cout<<hmap[a2]<<endl;        return 0;}





C++ STL中的雜湊表 hash_map

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.