Using an unordered_map with arrays as keys

Adrien picture Adrien · Mar 9, 2017 · Viewed 10.6k times · Source

I don't understand why I can't have an unordered_map with an array<int,3> as the key type:

#include <unordered_map>

using namespace std;

int main() {

   array<int,3> key = {0,1,2};

   unordered_map< array<int,3> , int >  test;
   test[key]  = 2;

   return 0;
}

I get a long error, the most pertinent part being

main.cpp:11:9: error: no match for ‘operator[]’ (operand types are std::unordered_map<std::array<int, 3ul>, int>’ and ‘std::array<int, 3ul>’)
 test[key]  = 2;
     ^

Are arrays not eligible to be keys because they miss some requirements?

Answer

Shridhar R Kulkarni picture Shridhar R Kulkarni · Mar 9, 2017

Why?

As mentioned in http://www.cplusplus.com/reference/unordered_map/unordered_map/

Internally, the elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their key values (with a constant average time complexity on average).

Now as per your question we need to hash an array which has not been implemented internally in standard c++.

How to get over with it?

So if you want to map an array to a value you must implement your own std::hash http://en.cppreference.com/w/cpp/utility/hash for which you might get some help from C++ how to insert array into hash set?.

Some work around

If you are free to use boost then it can provide you with hashing of arrays and many other types. It basically uses hash_combine method for which you can have a look at http://www.boost.org/doc/libs/1_49_0/boost/functional/hash/hash.hpp.