c++11 std::hash 的使用


c++11 std::hash 的使用

 

原文轉自:http://zh.cppreference.com/w/cpp


在頭文件 <functional> 中定義
   
     
template<class Key >
struct hash;// not defined
  (C++11 起)
     

哈希模板定義一個函數對象,實現了散列函數。這個函數對象的實例定義一個operator()

1。接受一個參數的類型Key.

2。返回一個類型為size_t的值,表示該參數的哈希值.

3。調用時不會拋出異常.

4。若兩個參數k1k2相等,則std::hash<Key>()(k1)== std::hash<Key>()(k2).

5。若兩個不同的參數k1k2不相等,則std::hash<Key>()(k1)== std::hash<Key>()(k2)成立的概率應非常小,接近1.0/std::numeric_limits<size_t>::max().

哈希模板是CopyConstructibleDestructible.

的無序關聯式容器std::unordered_set,std::unordered_multiset,std::unordered_map,std::unordered_multimap使用專業的模板std::hash默認的散列函數.

[編輯]會員類型

 
argument_type Key
 
result_type std::size_t

[編輯]成員函數

 
構造一個哈希函數對象
原文:
constructs a hash function object
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊 這里

(公共成員函數)
 
計算哈希的說法
原文:
calculate the hash of the argument
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊 這里

(公共成員函數)

[編輯]標准的專業基本類型

在頭文件 <functional> 中定義
   
     
template<>struct hash<bool>;

template<>struct hash<char>;
template<>struct hash<signedchar>;
template<>struct hash<unsignedchar>;
template<>struct hash<char16_t>;
template<>struct hash<char32_t>;
template<>struct hash<wchar_t>;
template<>struct hash<short>;
template<>struct hash<unsignedshort>;
template<>struct hash<int>;
template<>struct hash<unsignedint>;
template<>struct hash<long>;
template<>struct hash<longlong>;
template<>struct hash<unsignedlong>;
template<>struct hash<unsignedlong long>;
template<>struct hash<float>;
template<>struct hash<double>;
template<>struct hash<longdouble>;

template < class T > struct hash <T * > ;
   
     

[編輯]標准庫類型專業

 
字符串哈希支持
原文:
hash support for strings
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊 這里

(類模板特化) [edit]
 
std::error_code的哈希支持
原文:
hash support for std::error_code
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊 這里

(類模板特化) [edit]
 
std::bitset的哈希支持
原文:
hash support for std::bitset
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊 這里

(類模板特化) [edit]
 
std::unique_ptr的哈希支持
原文:
hash support for std::unique_ptr
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊 這里

(類模板特化) [edit]
 
std::shared_ptr的哈希支持
原文:
hash support for std::shared_ptr
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊 這里

(類模板特化) [edit]
 
std::type_index的哈希支持
原文:
hash support for std::type_index
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊 這里

(類模板特化) [edit]
 
std::vector<bool>的哈希支持
原文:
hash support for std::vector<bool>
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊 這里

(類模板特化)
 
std::thread::id的哈希支持
原文:
hash support for std::thread::id
這段文字是通過 Google Translate 自動翻譯生成的。
您可以幫助我們檢查、糾正翻譯中的錯誤。詳情請點擊 這里

(類模板特化)

[編輯]的例子

演示std::string,已經有一個哈希專業化的類型的哈希計算.

原文:

Demonstrates the computation of a hash for std::string, a type that already has a hash specialization.

#include <iostream> #include <functional> #include <string>   int main() { std::string str = "Meet the new boss..."; std::hash<std::string> hash_fn; size_t str_hash = hash_fn(str);   std::cout << str_hash << '\n'; }

輸出:

391070135

演示如何創建一個用戶定義的類型的哈希函數。以此為模板參數std::unordered_map,std::unordered_set等也需要專業化的std::equal_to.

原文:

Demonstrates creation of a hash function for a user defined type. Using this as a template parameter forstd::unordered_map, std::unordered_set, etc. also requires specialization ofstd::equal_to.

#include <iostream> #include <functional> #include <string>   struct S { std::string first_name; std::string last_name; };   template<class T> class MyHash;   template<> class MyHash<S> { public: size_t operator()(const S &s) const { size_t h1 = std::hash<std::string>()(s.first_name); size_t h2 = std::hash<std::string>()(s.last_name); return h1 ^ (h2 << 1); } };   int main() { std::string s1 = "Hubert"; std::string s2 = "Farnsworth"; std::hash<std::string> h1;   S n1; n1.first_name = s1; n1.last_name = s2;   std::cout << "hash(s1) = " << h1(s1) << "\n" << "hash(s2) = " << std::hash<std::string>()(s2) << "\n" << "hash(n1) = " << MyHash<S>()(n1) << "\n";   }

輸出:

hash(s1) = 6119893563398376542
hash(s2) = 14988020022735710972
hash(n1) = 17649170831080298918

 

 

 

演示如何專門std::hash為用戶定義的類型.

原文:

Demonstrates how to specialize std::hash for a user defined type.

#include <iostream> #include <functional> #include <string>   struct S { std::string first_name; std::string last_name; };   namespace std { template<> class hash<S> { public: size_t operator()(const S &s) const { size_t h1 = std::hash<std::string>()(s.first_name); size_t h2 = std::hash<std::string>()(s.last_name); return h1 ^ ( h2 << 1 ); } }; }   int main() { S s; s.first_name = "Bender"; s.last_name = "Rodriguez"; std::hash<S> hash_fn;   std::cout << "hash(s) = " << hash_fn(s) << "\n"; }

輸出:

hash(s) = 32902390710


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM