为了支持用户定义的键类型std::unordered_set<Key>和std::unordered_map<Key, Value> 一个具有提供operator==(Key, Key)和散列函子:
struct X { int id; /* ... */ };
bool operator==(X a, X b) { return a.id == b.id; }
struct MyHash {
size_t operator()(const X& x) const { return std::hash<int>()(x.id); }
};
std::unordered_set<X, MyHash> s;
仅std::unordered_set<X> 使用type 的默认哈希值编写将更方便X,例如与编译器和库一起提供的类型。经过咨询
C ++标准草案N3242§20.8.12 [unord.hash]和§17.6.3.4[hash.requirements],
增强无序
g ++ include\c++\4.7.0\bits\functional_hash.h
VC10 include\xfunctional
堆栈溢出中的各种相关问题
似乎可以专长std::hash<X>::operator():
namespace std { // argh!
template <>
inline size_t
hash<X>::operator()(const X& x) const { return hash<int>()(x.id); } // works for MS VC10, but not for g++
// or
// hash<X>::operator()(X x) const { return hash<int>()(x.id); } // works for g++ 4.7, but not for VC10
}
鉴于编译器对C ++ 11的支持尚处于试验阶段---我没有尝试过Clang ---,这是我的问题:
向名称空间添加这样的专业化合法std吗?我对此有百感交集。
哪个std::hash<X>::operator()版本(如果有)符合C ++ 11标准?
有便携式的方法吗?
守候你守候我
慕侠2389804
繁星淼淼
相关分类