![](/img/trans.png)
[英]Unordered_map of unordered_map vs custom hash function for pair key C++?
[英]unordered_map::find with key std::pair of pointers with custom hash crashes in VS2012
我需要一个带有键的std::unordered_map
std::pair<T*, T*>
所以我“偷”了下面的代码:
template <class T>
inline void hash_combine(std::size_t & seed, const T & v)
{
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
namespace std
{
template<typename S, typename T> struct hash<pair<S, T>>
{
inline size_t operator()(const pair<S, T> & v) const
{
size_t seed = 0;
::hash_combine(seed, v.first);
::hash_combine(seed, v.second);
return seed;
}
};
}
从这个stackoverflow 答案 。
它在gcc 4.9.2的linux机器上就像一个魅力。 但是在windows visual studio 2012中,它在调用我的unordered_map
成员函数find()
时崩溃了。 我的一个朋友调试了Windows机器上的崩溃,他报告说它只是在调试编译模式下通过给出“矢量下标超出范围”而中断。
std::pair<T*, T*>
? std::pair<T*, T*>
? PS:非常抱歉没有发布mcve,但这是不可能的。
在模板专业化std
也为各类std
可能会或可能不会让你的程序形成不良的(标准是模糊的,似乎在多种不同的方式使用“用户定义类型”而没有定义它)。 请参阅我关于此主题的问题 ,以及有关工作组的问题 。
因此,创建自己的哈希命名空间:
namespace my_hash {
template<class T=void,class=void>
struct hasher:std::hash<T>{};
template<class T, class=std::result_of_t< hasher<T>(T const&) >>
size_t hash( T const& t ) {
return hasher<T>{}(t);
}
template<>
struct hasher<void,void> {
template<class T>
std::result_of_t<hasher<T>(T const&)>
operator()(T const& t)const{
return hasher<T>{}(t);
}
};
// support for containers and tuples:
template <class T>
size_t hash_combine(std::size_t seed, const T & v) {
seed ^= hash(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
return seed;
}
template<class Tuple, size_t...Is>
size_t hash_tuple_like(Tuple const& t, size_t count, std::index_sequence<Is...>) {
size_t seed = hash(count);
using discard=int[];
(void)discard{0,((
seed = hash_combine(seed, std::get<Is>(t))
),void(),0)...};
return seed;
}
template<class Tuple>
size_t hash_tuple_like(Tuple const& t) {
constexpr size_t count = std::tuple_size<Tuple>{};
return hash_tuple_like(t, count, std::make_index_sequence<count>{} );
}
struct tuple_hasher {
template<class Tuple>
size_t operator()(Tuple const& t)const{
return hash_tuple_like(t);
}
};
template<class...Ts>
struct hasher<std::tuple<Ts...>,void>:
tuple_hasher
{};
template<class T, size_t N>
struct hasher<std::array<T,N>,void>:
tuple_hasher
{};
template<class...Ts>
struct hasher<std::pair<Ts...>,void>:
tuple_hasher
{};
template<class C>
size_t hash_container( C const& c ) {
size_t seed = hash(c.size());
for( const auto& x:c ) {
seed = hash_combine( seed, x );
}
return seed;
}
struct container_hasher {
template<class C>
size_t operator()(C const& c)const{ return hash_container(c); }
};
template<class...Ts>
struct hasher< std::vector<Ts...>, void >:
container_hasher
{};
// etc
};
现在你将my_hash::hasher<>
作为你的hasher传递给一个容器,你不必做一个粗略的业务,即在std
为一个类型(主要是)提供std
。
my_hash::hasher<?,void>
存在,所以你可以做SFINAE测试(比如,检测一个类型为容器状,并转发到hash_container
。 my_hash::hash
提供ADL压倒一切的类型,而不必在鬼混my_hash
命名空间。
举个例子:
template<class T>
struct custom {
std::vector<T> state;
friend size_t hash( custom const& c ) {
using my_hash::hash;
return hash(state);
}
};
和custom
现在可以清洗。 不需要杂乱的专业化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.