0

I have two integers and I want to create a hash map which maps the two integers to another integer.

In order to so I created the following program, however it is giving me errors:

#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;
}

Is there some way to get rid of the errors? Is there some way to do the same in a scalable manner?

Error:

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:
Alice Everett
  • 375
  • 1
  • 7
  • 15

1 Answers1

3

I assume the error is something like "The C++ Standard doesn't provide a hash for this type"? The problem is that anything you use as the key in an unordered map must be hashable, and there is no built-in hash function for pair<int, int>. A couple possibilities are:

-Write your own hasher for pair<int, int> and use it as the third template parameter to the unordered_map. This post may be able to help with that.

-Use std::unordered_map<int, std::unordered_map<int, int>> instead. Be careful not to accidentally create copies of the inner map when you're performing operations.

-Use map instead of unordered_map. This will give you logarithmic operations instead of constant-time, but unless the map is huge and you're doing tons of lookups per second, you won't notice the difference.

Edit in response to your edit: Yes, that's what those error messages amount to, though they don't say it in quite as friendly a fashion as Microsoft's compiler does. :)

Community
  • 1
  • 1
dlf
  • 9,045
  • 4
  • 32
  • 58
  • There is no need to pass the hasher as the third parameter, OP can specialize the `std::hash`template and it will be loaded automatically -- see http://ideone.com/4buTej – Erbureth Apr 24 '14 at 13:40