我必须编写一个散列函数,以便我可以将std::pair<int,std::string>
放入unordered_set
。散列字符串和int在一起?
关于输入:
- 将被散列的串是非常小的(1-3字母长度)。
- 同样,整数将是小号的无符号数(远小于无符号整数的极限)。
是否有意义使用字符串的散列(作为数字),并且只使用Cantor的枚举对来生成“新”散列?
由于为std::string
的“内置”哈希函数应该是一个体面的哈希函数...
struct intStringHash{
public:
inline std::size_t operator()(const std::pair<int,std::string>&c)const{
int x = c.first;
std::string s = c.second;
std::hash<std::string> stringHash;
int y = stringHash(s);
return ((x+y)*(x+y+1)/2 + y); // Cantor's enumeration of pairs
}
};
您可以'提振:: hash_combine',或者,如果你不能使用升压出于某种原因,检查他们做了什么,并复制 – milleniumbug
我不能使用boost代码。你能解释一下怎么做吗?我读过另一篇关于创建函数的帖子http://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x,但我不知道如何使用它关于我的上面的函数? –