在visual studio下使用pair作为hash_map的键

use*_*574 5 c++ hashmap std-pair

尝试在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;
}
Run Code Online (Sandbox Code Playgroud)

收到错误信息:

错误1错误C2440:'type cast':无法从'const std :: pair <_Ty1,_Ty2>'转换为'size_t'c:\ program files\microsoft visual studio 10.0\vc\include\xhash 34 1 testApplication1

我知道它可能是由于hash_mapddoes没有提供专业化pair.有什么简单的方法来解决它吗?谢谢

小智 4

您必须hash_compare为用作键的对象编写自己的函数!

你的情况是这样的std::pair<int,int>

看看这篇文章 - 也许你会得到一个更好的主意来实现你自己的比较器!