为什么哈希<常量字符*>适用于字符串而不是字符串变量?

Why does hash<const char*> work for strings but not string variables?

本文关键字:字符串 适用于 变量 lt 哈希 常量 字符 为什么 gt      更新时间:2023-10-16

我的问题是为什么以下代码有效

hash<const char*> PassHash;
cout << PassHash("Hello World");

但是此代码无法编译。

hash<const char*> PassHash;
string password;
password = "Hello World";
cout << PassHash(password);

在代码块中,我收到此错误

error: no match for call to '(__gnu_cxx::hash<const char*>) (std::string&)'|

std::hash的定义类似于以下内容

template<typename T>
struct hash {
size_t operator()(const T& value) const {
...
}
}

因此,std::hash<const char*>模板实例化定义了一个接受const char*operator(),但您传递的是一个不同类型的std::string,这很简单。

只需直接使用std::string作为您的密码变量,然后改为std::hash<std::string>

没有从std::stringconst char*的隐式转换,这是使您的示例正常工作所必需的。

您可以致电string::c_str()明确执行此"转换"...

hash<const char*> PassHash;
string password;
password = "Hello World";
cout << PassHash(password.c_str());

。但这只会计算字符串指针的哈希值,因为hash<const char*>没有专用化!因此,这仅与通用指针专用化hash<T*>匹配。

您可能真正想要的是字符串的整个字符数组的哈希值,因此如果字符串的一个字符发生变化,您(很可能)会得到不同的哈希值。

为此,您可以使用hash<std::string>专业化。这适用于const char*std::string参数,因为 std::string 有一个采用const char*的转换构造函数。

例:

const char* password1 = "Hello World";
string password2 = "Hello World";
hash<const char*> charPtrHasher;
// This only calculates a hash from the value of the pointer, not from 
// the actual string data! This is why you get a different hash for each.
cout << "Test 1:n";
cout << charPtrHasher(password1) << endl << charPtrHasher(password2.c_str()) << endl;
hash<std::string> stringHasher;
// This correctly calculates the hash over all characters of the string!
cout << "nTest 2:n";
cout << stringHasher(password1) << endl << stringHasher(password2) << endl;

现场演示:http://coliru.stacked-crooked.com/a/047c099f5dcff948