Reputation: 35
In order to map a group of integer variables to values (say I have a1, a2, a3, a4, a5, a6
to determine a value v
; that's a map like map<tuple<int, int, int, int, int, int>,VALUE_TYPE>
or map<struct{int, int, int, int, int, int},VALUE_TYPE>
), we may use
I am very curious about the performance of these methods. In my situation,
The question is which way performs better in my situation?
If struct or class key chosen, is there any trick to make comparator more efficient?
Does unordered_map fit the situation better? How should I calculate hash of keys?
I will appreciate a lot to any suggestion or comment on solutions. And discussion on a more general situation is also welcomed. Thanks in advance.
Upvotes: 2
Views: 1877
Reputation: 6440
Also, consider implementing the trie data structure if your numbers are relatively small and you have a lot of them in a key. Just like std::unordered_map
this will allow you to access the element in O(l)
time where l
is a length of the key, but also it will allow you to iterate throw all the values in ascending order and find a value by a partial key. The problem here is that your memory consumption will be proportional to the alphabet size (of course, you can store the inner edges in some complex data structure, but this will negatively affect performance).
Upvotes: 0
Reputation: 2474
Basically: Implement the different solutions, write a performance test program and measure!
For sure:
std::array<>
container (already provides operators for comparison too).std::unordered_map<>
) is faster than a sorted map (std::map<>
), but can of course only be used if you don't need to search with partial keys.Upvotes: 4