gcc-3.0.2 + hash_map<string,int> compilation problems

Phil Edwards phil@jaj.com
Mon Apr 8 18:10:00 GMT 2002

On Mon, Apr 08, 2002 at 11:42:45PM +0200, Stefan Olsson wrote:
> I have had the same issue and solved it by declaring:
> #include <string>
> #include <ext/hash_map>
> namespace __gnu_cxx
> {
>         template<> struct hash< std::string >
>         {
>                 size_t operator()( const std::string& x ) const
>                 {
>                         return hash< const char* >()( x.c_str() );
>                 }
>         };
> }

That's definitely one way to do it; if you look at ext/stl_hash_fun.h you'll
see what's happening here.  You could return __stl_hash_string directly.
If you're going to use a nonportable extension, why stop with just one?  :-)

> I guess that the reason for not having this declared in the std::string 
> is that the hash* are extensions to the standard.

I'm assuming you meant /for/ std::string rather than /in/ std::string.

Take a look at the notes from authors of the hash* stuff:


> Maybe something like 
> the above could be added into the codebase somewhere else?

As the second link points out, the default hashing function is only a guess.
You are encouraged to write your own and use it instead of __stl_hash_string.


If ye love wealth greater than liberty, the tranquility of servitude greater
than the animating contest for freedom, go home and leave us in peace.  We seek
not your counsel, nor your arms.  Crouch down and lick the hand that feeds you;
and may posterity forget that ye were our countrymen.            - Samuel Adams

More information about the Libstdc++ mailing list