0

There is a great example of a simple hash table in C++ for a single key, but I would like to hash on a <int, double> combination so that, for example h[5, 0.1] will return a double. Is this possible?

One possible method to get around this is to create an array of unordered_maps, and then have the key be a double. So, for example, I could simply call h[5][0.1] and get the double value back. In this the best way to go about this or can I create a multi-variabled key?

Community
  • 1
  • 1
drjrm3
  • 4,474
  • 10
  • 53
  • 91

1 Answers1

1

Sure. Make your key std::pair<int, double> (or tuple of <int, double>). Define appropriate hashing function (I would say hash(int) ^ hash(double) might work)

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
  • So I would still need to define a `R X R -> R` function for the hash function to compute the value? I am trying to use this hash map to avoid computation. If I have computed f(a,b), then store the value and just hash for it later, but if I need to compute f(a,b) to use as the key, this defeats the purpose. – drjrm3 Jan 19 '16 at 14:50
  • @Laurbert515 - The `unordered_map` in you example uses `std::hash` internally. If you have both `int` and `double` in your key, you will have to provide a hasher that combines `std::hash` and `std::hash`. That's it. – Bo Persson Jan 19 '16 at 14:55
  • @Laurbert515 As myself and Bo proposed, you have hash implementation for int and double already. So just write one liner which combines them, I proposed using ^, there are other alternatives like http://www.boost.org/doc/libs/1_35_0/doc/html/boost/hash_combine_id241013.html – Severin Pappadeux Jan 19 '16 at 15:07