2014-04-24 79 views
1

我有两个整数,我想创建一个哈希映射,将两个整数映射到另一个整数。将两个整数映射到另一个整数

为了让我创建了下面的程序,但它给我的错误:

#include <iostream> 
#include <unordered_map> 

using namespace std; 

int main() 
{ 
    std::pair <int, int> var1; 
    var1=std::make_pair(10,20); 
    cout<<"\n var1.f="<<var1.first<<"\t 2."<<var1.second<<"\n"; 
    std::unordered_map <std::pair <int,int>, int> yeah; 

    return 0; 
} 

是否有某种方式来摆脱错误的? 有没有办法以可扩展的方式做同样的事情?

错误:

n file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:35:0, 
       from /usr/local/gcc-4.8.1/include/c++/4.8.1/unordered_map:47, 
       from main.cpp:2: 
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h: In instantiation of ‘struct std::__detail::_Hash_code_base<std::pair<int, int>, std::pair<const std::pair<int, int>, int>, std::__detail::_Select1st, std::hash<std::pair<int, int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>’: 
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:1402:10: required from ‘struct std::__detail::_Hashtable_base<std::pair<int, int>, std::pair<const std::pair<int, int>, int>, std::__detail::_Select1st, std::equal_to<std::pair<int, int> >, std::hash<std::pair<int, int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true> >’ 
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:174:11: required from ‘class std::_Hashtable<std::pair<int, int>, std::pair<const std::pair<int, int>, int>, std::allocator<std::pair<const std::pair<int, int>, int> >, std::__detail::_Select1st, std::equal_to<std::pair<int, int> >, std::hash<std::pair<int, int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >’ 
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/unordered_map.h:100:18: required from ‘class std::unordered_map<std::pair<int, int>, int>’ 
main.cpp:12:50: required from here 
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:1070:12: error: invalid use of incomplete type ‘struct std::hash<std::pair<int, int> >’ 
    struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, 
      ^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.h:3033:0, 
       from /usr/local/gcc-4.8.1/include/c++/4.8.1/string:52, 
       from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/locale_classes.h:40, 
       from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/ios_base.h:41, 
       from /usr/local/gcc-4.8.1/include/c++/4.8.1/ios:42, 
       from /usr/local/gcc-4.8.1/include/c++/4.8.1/ostream:38, 
       from /usr/local/gcc-4.8.1/include/c++/4.8.1/iostream:39, 
       from main.cpp:1: 
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/functional_hash.h:58:12: error: declaration of ‘struct std::hash<std::pair<int, int> >’ 
    struct hash; 
      ^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:35:0, 
       from /usr/local/gcc-4.8.1/include/c++/4.8.1/unordered_map:47, 
       from main.cpp:2: 
+1

删除'c'标签。 –

+0

你为什么要标记这个C?这显然不是C! –

+3

什么是不可扩展的这种解决方案? –

回答

3

我假设的错误是一样的东西“C++标准不提供这种类型的哈希”?问题在于,你在无序映射中用作键的任何东西都必须是可散列的,并且pair<int, int>没有内置散列函数。有几种可能性:

- 为pair<int, int>写下自己的散列器,并将其用作unordered_map的第三个模板参数。 This post也许能够帮上忙。

- 改为使用std::unordered_map<int, std::unordered_map<int, int>>。小心不要在执行操作时意外创建内部映射的副本。使用map代替unordered_map。这会给你对数操作而不是常量,但除非地图很大,而且你每秒钟做大量的查找,否则你不会注意到这种差异。

编辑回应你的编辑:是的,这就是那些错误信息的总和,虽然他们没有像微软的编译器那样友好地说出它。 :)

+0

没有必要通过散列器作为第三个参数,OP可以专门化std :: hash模板,它将自动加载 - 参见http://ideone.com/4buTej – Erbureth

+0

@Erbureth谢谢;我不知道那件事。 – dlf

相关问题