Re: hash_map
AlbertSSj wrote:
BTW, you need specialized hask<std::string> to
work with g++, IIRC.
A related question...
To specialize hash std::string this works...
namespace __gnu_cxx {
template<> struct hash<std::string>
{
size_t operator()(const std::string& __s) const {
return __stl_hash_string(__s.c_str());
}
};
}
And it actually uses the hash for const char * in
stl_hash_fun.h
My question is...
It is ok in to write such code?
No. At least, not if you consider __gnu_cxx to obey the rules
of the standard library.
It compiles and works... But everywere is said to not add
something in the standard library (for a lot of good reasons).
Well, you're explicitly allowed to add specializations of
standard functions and classes for types you define. And of
course, it's inconceivable that the standard won't define a
specialization for std::string. So when you switch to the
standard version, suddenly, you'll find yourself with two
specializations of the same thing.
It is better to use something like this?
struct str_hash {
size_t operator()(const std::string& __s) const {
return ::__gnu_cxx::__stl_hash_string(__s.c_str());
}
};
}
There's less risk.
Or maybe __stl_hash_string should not be called at all?
Given that the hash function it uses has some noticeably
weaknesses, that might be the best idea of all:-). (Seriously,
the g++ uses the same hashing function as Java, and is almost
certainly sufficient for most use.)
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]