c++ - 在 Visual Studio 下使用 pair 作为 hash_map 的键

标签 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;
}

收到错误信息:

Error 1 error C2440: 'type cast' : cannot convert from 'const std::pair<_Ty1,_Ty2>' to 'size_t' c:\program files\microsoft visual studio 10.0\vc\include\xhash 34 1 testApplication1

我知道这可能是因为 hash_map 没有为 pair 提供专门化。有什么简单的方法可以解决吗?

最佳答案

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

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

this帖子 - 也许您对实现自己的比较器有更好的想法!

关于c++ - 在 Visual Studio 下使用 pair 作为 hash_map 的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14878721/

相关文章:

c++ - 提取 32 位长的中间 16 位

java - 在 JSP 中获取 Map<String, List<String>> 中列表的第一个元素

java - Hadoop 中的 HashMap

java - 有没有HashMap连接元素

c++ - std::allocator_traits::construct 调用了错误的构造函数

c++ - 排序 vector 对 : No matching function

c++ - 如何为类成员变量定义threadprivate变量

c++ - 删除指向数组的指针

C++ - 构造 ICMP 数据包时出现访问冲突

c++ - 检查对是否为空或未初始化