8

My line:

unordered_map < pair<long long,long long>, long long> myMap;

And the error:

error: no matching function for call to 'std::unordered_map<std::pair<long long int, long long int>, long long int>::unordered_map()'

Code that reproduces the error:

#include <math.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <utility>
using namespace std;

unordered_map < pair<long long,long long>, long long> myMap;

int main() {
    return 0;
}
5
  • Did you #include <unordered_map> and #include <utility>? Commented Aug 31, 2015 at 18:48
  • I included both, yes. Commented Aug 31, 2015 at 18:49
  • 4
    Please include a copy of a minimal working example of your code to help people better diagnose your problem Commented Aug 31, 2015 at 18:49
  • Please edit your question with an minimal reproducible example or SSCCE (Short, Self Contained, Correct Example) Commented Aug 31, 2015 at 18:50
  • 1
    With another compiler, the message is "The C++ Standard doesn't provide a hash for this type.". Meaning std::pair. Commented Aug 31, 2015 at 18:55

1 Answer 1

10

std::unordered_map requires a hash functor in order to do anything. By default, that hash functor is is std::hash<Key> - but the standard only provides specializations for the integral types and pointers. std::pair<long long, long long> is neither, so effectively the compiler is telling you that you cannot instantiate the unordered_map you want because its hash functor is ill-formed.

What you need to do is provide your own. For example, here is the worst possible hash functor:

struct AlwaysZero {
    size_t operator()(pair<long long, long long> const& ) const {
        return 0;
    }
};

unordered_map < pair<long long,long long>, long long, AlwaysZero> myMap;

That compiles. It will also perform terribly. You could check out boost::hash_combine to get an idea of how to correctly write a much better hash.

Sign up to request clarification or add additional context in comments.

9 Comments

It's possible that I am asking an XY question. Is there a better way to do a lookup by two variables? I'm basically trying to create a map where I feed it (a,b) and it returns c, and as efficiently as possible to minimize overall runtime.
I also find this strange because using pairs works with map but not unordered_map
@user64283 Having a pair<a,b> for the key sounds reasonable to me if that's what you're doing. Your problem is just that the standard library doesn't support that by default.
@user64283 That sounds terrible.
@user64283 Because it's a hack. You want to look up (a,b) so look up (a,b), don't look up str(a) + " " + str(b). And doing math with numbers is muuuuch faster than doing math with strings. And you don't need to deal with memory allocation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.