.NET framework has got a Dictionary<TKey,TValue> class which is implemented as hash tables and provides data retrieval in constant time (O(1)). I am looking for a similar implementation in C++. I know about std::map but in this data retrieval takes logarithmic time. Is there any good hash table implementation in C++ which will retrieve data in constant time?
If I am writing my own, how will I calculate hash code for the key? Like .NET, I thought of having GetHashCode() method on types.
template<typename TKey,typename TVal>
class Dictionary
{
public:
void Add(TKey key, TVal val){
int hashCode = key.GetHashCode();
/* .... */
}
}
If I did like the above and the given key type doesn't have GetHashCode() method, compiler will throw error. But this method won't work when key is primitive types like int. I may need to write a wrapper for int by providing GetHashCode.
I wonder what is the C++ way of implementing this?
Any thoughts?