0

I'm trying to make an unordered_map, in which the key is vector and the value is int. I can't use a map, because the time complexity for access is O(log n), while I need O(1). However, unordered_map pops out this error:

error: static assertion failed: hash function must be invocable with an argument of key type

what should I do?

Kaja
  • 1
  • https://stackoverflow.com/questions/20511347/a-good-hash-function-for-a-vector – Retired Ninja Nov 20 '21 at 18:37
  • Supply a hash function for a `vector`. This sounds like a generally bad idea, but could make sense in the right circumstances. – super Nov 20 '21 at 18:37
  • While the other comments suggesting a hash function implementation are not wrong, it would be highly unusual for a vector to be key in a map. Perhaps you really just want to make the pointer to the vector be the key? **What are you really trying to do?** – selbie Nov 20 '21 at 18:38
  • Thanks for the answers! But I'm still not sure if my idea is the most efficient way. I'm trying to make a map, which would help me with memoization. I want to save particular permutation of natural numbers from 1 to n, which i put onto vector, as the key. For example: {1,2,3,4} is one permutation and {1,3,2,4} is another one. Do you have any other idea or any other data structure with access with time complexity O(1) in which I could save those permutations? – Kaja Nov 20 '21 at 18:50
  • There are `N!` unique permutations for N numbers. You would be better off storing the index value from 0 to N!-1 as the key. Because you can always derive the unique permutation (vector) from that index value if you need it. For example {1,2,3,4} is permutation number 0. {2,1,3,4} is permutation number 1. {3,1,2,4} is number 3.... and {4,3,2,1} might be number 23. But you never set what you would do with the integer map's value once you looked it up in the map. What is the actual dynamic programming problem you are trying to solve? – selbie Nov 20 '21 at 19:20

0 Answers0