C++雜湊函數應用執行個體

來源:互聯網
上載者:User

#include "stdafx.h"  #include <iostream>  #include <hash_map>  #include <vector>using std::vector;  using stdext::hash_map;typedef unsigned int        UINT;class  hash_wchar_t  {  public:  // 以下兩個變數我也不是很明白究竟是幹嘛的  static const size_t   bucket_size = 4;   // 猜測這個變數是初始化hash_map的大小  static const size_t   min_buckets = 8;   // 猜測這個變數是每次擴充容量的大小  // 以上猜測是根據vector得來的,其實我基本上沒使用過STL,只是在C++Primer上看到過,很粗略的看。</p><p> size_t operator()(const   wchar_t&   GBword) const         {  return GBword%100;      // 下面的那個雜湊函數演算法是我在網上搜尋的說是適合漢字使用的。  // 具體適不適合我也不知道,這裡測試的時候可以用簡單的  // return ((unsigned char)GBword-176)*94 + (unsigned char)(GBword>>8) - 161;         }   bool operator()(const wchar_t& s1,const wchar_t& s2) const         {    // copy別人代碼的時候,由於Key類型是char類型字串,所以是這麼寫的  // return 0 == strcmp(s1,s2);  // 我針對自己使用的Key類型,在修改了參數的形式之後,很天真的就這麼使用,這是問題的關鍵!!  // 寫成這種形式,在下面 測試能否找到的時候,始終出問題,  // 原因是p指標指向的是一個未初始化的記憶體地區,所以無法取資料  // 具體原理在代碼後面解釋  return s1 == s2;  // </p><p>  // 最後的正確用法  // return s1 < s2;   // 或者 return s2 > s1;  }    }; int main()  {  hash_map<const wchar_t,vector<UINT>*,hash_wchar_t> loNameMap;  vector<UINT>* lpoVecUint = NULL;  lpoVecUint = new vector<UINT>;  lpoVecUint->push_back(2);loNameMap[L'C'] = lpoVecUint;  loNameMap[L'A'] = lpoVecUint;  loNameMap[L'B'] = lpoVecUint;vector<UINT>* p = loNameMap[L'A'];   // 測試能否找到  std::cout<<p->size()<<std::endl;  std::cout<<p->at(0)<<std::endl;    getchar();return 1;  }  

轉載自:

http://blog.csdn.net/a2619308/article/details/6816989

聯繫我們

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