為什麼我不能編譯一個以pair為key的unordered_map?
這裡面臨的問題是缺乏合適的雜湊函數對於金鑰類型。若要解決此問題,請為金鑰對提供自訂雜湊函數。這是一個範例:
#include <unordered_map> #include <functional> #include <string> #include <utility> struct pair_hash { template <class t1 class t2> std::size_t operator() (const std::pair<t1>& p) const { auto h1 = std::hash<t1>{}(p.first); auto h2 = std::hash<t2>{}(p.second); return h1 ^ h2; // Simple example, for better results use boost.hash_combine } }; using Vote = std::pair<:string std::string>; using Unordered_map = std::unordered_map<vote int pair_hash>;</vote></:string></t2></t1></t1></class></utility></string></functional></unordered_map>
使用此自訂雜湊函數,您現在可以建立一個 unordered_map
以上是為什麼我不能使用 `std::pair` 作為 `std::unordered_map` 中的鍵以及如何修復它?的詳細內容。更多資訊請關注PHP中文網其他相關文章!