簡體   English   中英

對 <int,int> 配對作為unordered_map問題的鍵

[英]pair<int,int> pair as key of unordered_map issue

我的代碼:

 typedef pair<int,int> Pair
  tr1::unordered_map<Pair,bool> h;
  h.insert(make_pair(Pair(0,0),true));

錯誤

 undefined reference to `std::tr1::hash<std::pair<int, int> >::operator()(std::pair<int, int>) const'

我需要修復什么?

謝謝

發生這種情況是因為std::tr1::hash<Key>Key = std::pair<int, int>沒有專門化的地方。 在聲明tr1::unordered_map<Pair,bool> h;之前Key = std::pair<int, int>必須使用Key = std::pair<int, int>專門化std::tr1::hash<Key> tr1::unordered_map<Pair,bool> h; 發生這種情況是因為std不知道如何pair<int, int>進行哈希處理。

以下是如何專門化std::tr1::hash<>的示例

template <>
struct std::tr1::hash<std::pair<int, int> > {
public:
        size_t operator()(std::pair<int, int> x) const throw() {
             size_t h = SOMETHING;//something with x   
             return h;
        }
};

無序映射不包含一個對的哈希函數,因此,如果我們要哈希一個對,則必須顯式地為其提供可以哈希一個對的哈希函數。

如果我們要使用對作為unordered_map的鍵,則有兩種方法可以實現:

  1. 為std :: hash定義特殊化
typedef std::pair<std::string,std::string> pair;

struct pair_hash
{
    template <class T1, class T2>
    std::size_t operator() (const std::pair<T1, T2> &pair) const
    {
        return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
    }
};

int main()
{
    std::unordered_map<pair,int,pair_hash> unordered_map =
    {
        {{"C++", "C++11"}, 2011},
        {{"C++", "C++14"}, 2014},
        {{"C++", "C++17"}, 2017},
        {{"Java", "Java 7"}, 2011},
        {{"Java", "Java 8"}, 2014},
        {{"Java", "Java 9"}, 2017}
    };

    for (auto const &entry: unordered_map)
    {
        auto key_pair = entry.first;
        std::cout << "{" << key_pair.first << "," << key_pair.second << "}, "
                  << entry.second << '\n';
    }

    return 0;
}
  1. 使用Boost庫另一個好方法是使用Boost.functional中的boost :: hash,該哈希可用於哈希整數,浮點數,指針,字符串,數組,對和STL容器。
#include <iostream>
#include <boost/functional/hash.hpp>
#include <unordered_map>
#include <utility>

typedef std::pair<std::string,std::string> pair;

int main()
{
    std::unordered_map<pair,int,boost::hash<pair>> unordered_map =
    {
        {{"C++", "C++11"}, 2011},
        {{"C++", "C++14"}, 2014},
        {{"C++", "C++17"}, 2017},
        {{"Java", "Java 7"}, 2011},
        {{"Java", "Java 8"}, 2014},
        {{"Java", "Java 9"}, 2017}
    };

    for (auto const &entry: unordered_map)
    {
        auto key_pair = entry.first;
        std::cout << "{" << key_pair.first << "," << key_pair.second << "}, "
                  << entry.second << '\n';
    }

    return 0;
}

遇到相同的問題:

unordered_map <pair<x, y>, z> m1;

一些解決方法是:

unordered_map <stringxy, z> m1;
// the first and second of the pair merged to a string
//   though string parsing may be required, looks same complexity overall

unordered_multimap <x, pair<y, z>> m1;
// second of the pair of the key went into value.  
//   time complexity slightly increases

deque<deque<x>> d1;
// here x & y are of same type, z is stored as: d1[x][y] = z
//   space required is x * y, however time complexity is O(1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM