我的代码:
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'
有什么需要我修复的吗?
谢谢
我的代码:
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'
有什么需要我修复的吗?
谢谢
Key = std::pair<int, int>
的 std::tr1::hash<Key>
专门化。在声明 tr1::unordered_map<Pair,bool> h;
前,您必须用 Key = std::pair<int, int>
对 std::tr1::hash<Key>
进行特殊化处理。这是因为 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;
}
};
无序映射不包含对于pair的哈希函数,如果我们想要哈希一个pair,则必须显式提供一个可以哈希pair的哈希函数。
如果我们想要将pair作为无序映射的键,有两种方法可以实现:
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;
}
#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)
unordered_multimap <x, pair<y, z>> m1;
这个定义足以插入两个成对的值 (x,y),其中 x 相同而 y 不同吗?如果值部分包含 y,那么哈希函数如何知道要使用它? - Rez
std::tr1::hash
有点不够明确,最好能够通过指定一个自定义的哈希类作为unordered_map
的第三个模板参数来解决这个问题。 - Steve Jessoptemplate <typename T, U> struct hash<pair<T,U> > { size_t operator() { return hash<T>()(first) ^ hash<U>()(second); }};
。只要T和U的hashcodes是有效的,这将为pair<T,U>
提供有效的哈希码。正如我所说,C++0x对于向量具有通用哈希函数,Java和Python也对集合具有通用哈希函数。因此,我不明白为什么TR1甚至C++0x都没有通用哈希对于pair,更不用说了。 - Steve Jessop