[英]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.