I want to know how stl hash_map is implemented. How do I find out what the table size is and the memory space the map consumes? This is in C++.
+1
A:
This blog entry on C++ STL Hash Containers and Performance is a good looking explanation of STL hash map. See if it helps you.
duffymo
2009-12-28 21:44:13
Doesn't tell me what the table size is :(.
SuperString
2009-12-28 21:48:31
It is, but I can also see it here: http://www.sgi.com/tech/stl/hash_map.html.
avakar
2009-12-28 22:01:40
I dont know why I am getting this:hash.cpp(247) : error C2039: 'bucket_count' : is not a member of 'stdext::hash_map<_Kty,_Ty>' with [ _Kty=std::string, _Ty=int ]
SuperString
2009-12-28 22:13:20
+2
A:
There is no such thing as an "stl hash_map". There is an unordered_map in TR1, but I assume you're not using that or you would have said unordered_map.
As someone pointed out, unordered_map has "bucket_count" to determine the number of buckets. You can iterate over each bucket, get it's size ("bucket_size(size_t bucket_num)"), multiply that by the size of a pair of key and values, and add them all up to give you a rough estimate of the memory used. There may be non-portable ways which are implementation defined. It will obviously be implemention defined for whatever hash_map class you're using.
Terry Mahaffey
2009-12-28 22:08:18
It is telling me bucket_count is not a member of the stdext: hash_map???
SuperString
2009-12-28 22:22:12
@SuperString As I said in the above answer: stdext::hash_map is old and busted. It's a Microsoft extension that is no longer maintained. Don't use it. Use std::tr1::unordered_map instead.
Terry Mahaffey
2009-12-28 22:25:51