Sign in to follow this  
ArnoAtWork

hashmap

Recommended Posts

I'am using std::hash_map for several manager of my system. When the key name used to specify a ressource is too big, the resulting hashed value is identical... Does STL uses the first caracters to hash? How can I modify this, or should I use a fixed name size? Thanks a lot.

Share this post


Link to post
Share on other sites
Even though it is prefixed by std:: hash_map is not part of c++ standard, so you have to tell us which C++ environment you are using.

Share this post


Link to post
Share on other sites
Quote:
Original post by ArnoAtWork
Does STL uses the first caracters to hash?


This is implementation dependent.

Quote:
Original post by ArnoAtWork
How can I modify this, or should I use a fixed name size?

....

I'am using stdext::hash_map of VC7.1.


VC++ 7.1 hash_map's interface is not exactly the same as SGI's and the one defined in C++ 0x03 TR1 standard, any ways its quite easy to provide a custom hasher for VC++ 7.1's hash_map you can use mine as an example or use it entirely up to you:


#include <cstddef>
#include <locale>
#include <string>

struct cust_string_comp {

const size_t bucket_size;
const size_t min_buckets;

cust_string_comp(): bucket_size(4), min_buckets(8) {}

size_t operator()(const std::string& Key) const {
const std::collate< char >& c = std::use_facet< std::collate< char > >(std::locale::classic());
return c.hash(Key.c_str(), Key.c_str() + Key.size());
}

bool operator()(const std::string& Key1, const std::string& Key2) const {
return Key1 < Key2;
}
};

#include <hash_map>

typedef stdext::hash_map< std::string, std::string, cust_string_comp > foo_bar_map;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this