I’d like to implement the following data structure in c++ (pseudo code):
Map<Integer, Integer> // Key->Value pairs
Map.put(1,6);
Map.put(2,5);
Map.put(6,89);
Map.put(7,23);
... etc ...
Map.get(2) .... returns 5
In other words, given pairs of integers, where one of them is a look-up key, what is the fastest library implementation that lets me retrieve the Value from one of the Keys? The opposite search of Value->Key is not required.
The size of this map would likely be on the order of 10 000 elements.
I assume a binary tree search will yield the fastest lookup time?
Is std:map the best tool to use? Does boost present any alternatives?
Use an
unordered_map(hashmap) ormap(binary tree) – Likelyunordered_mapwill be faster. Also, if your key value is limited to 10000, avector<int>will guarantee constant-time lookup – Use a “magic” value for vector elements that should be “not present”.unordered_mapis a part of TR1 and c++0x – it is not standard in c++03. Many implementations support it though. Boost also has anunordered_map.mapandvectorare both standard.mapcorresponds to javaTreeMapunordered_mapcorresponds to javaHashMapvectorcorresponds to javaArrayList