tags:

views:

288

answers:

3

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
Doesn't tell me what the table size is :(.
SuperString
A: 

Try bucket_count.

avakar
isnt that for unordered_set?
SuperString
It is, but I can also see it here: http://www.sgi.com/tech/stl/hash_map.html.
avakar
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
stdext::hash_map is old and busted. Don't use it.
Terry Mahaffey
+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
It is telling me bucket_count is not a member of the stdext: hash_map???
SuperString
@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