【发布时间】:2013-01-30 11:38:05
【问题描述】:
在 Visual Studio 2010 下尝试使用 pair 作为 hash_map 的键值。
无法编译。
int _tmain(int argc, _TCHAR* argv[])
{
hash_map <pair<int, int>, int> months;
months[pair<int, int>(2,3)] = 1;
int d;
cin >> d;
return 0;
}
收到错误消息:
错误 1 错误 C2440: 'type cast' : 无法从 'const std::pair<_ty1>' 转换为 'size_t' c:\program files\microsoft visual studio 10.0\vc\include\xhash 34 1测试应用程序1
我知道这可能是因为hash_map 没有为pair 提供专业化。有什么简单的方法可以解决吗?
【问题讨论】:
-
嗯,可以使用
std::map但不能使用std::unordered_map -
“在 Visual Studio 2010 下” - 删除
hash_map并使用正确的std::unordered_map。虽然,这仍然不能解决你的问题。不幸的是,缺少std::pair的哈希函数是 C++11 中最大的疏忽之一(但好吧,至少他们在 15 年后意识到哈希是有用的数据结构)。