C ++哈希用户定义的类型以便在无序映射中使用 - 我可以创建一个长字符串然后使用默认哈希吗? 就像标题所说的那样, 这会有用吗?
struct Key
{
std::string first;
std::string second;
int third;
bool operator==(const Key &other) const
{ return (first == other.first
&& second == other.second
&& third == other.third);
}
};
struct KeyHasher
{
std::size_t operator()(const Key& k) const
{
using std::size_t;
using std::hash;
using std::string;
return ((hash<string>()(k.first + k.second +(std::to_string(k.third));
}
};
int main()
{
std::unordered_map<Key,std::string,KeyHasher> m6 = {
{ {"John", "Doe", 12}, "example"},
{ {"Mary", "Sue", 21}, "another"}
};
}
它只会在长字符串上使用默认哈希吗?