For Programmers: Free Programming Magazines  


Home > Archive > VC STL > February 2005 > Problem with strings as hash_map keys









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with strings as hash_map keys
Richard Asbury

2005-02-14, 9:11 pm

I'm trying to use a std::string as a key to a std::hash_map - I would've
thought that this is possible without defining any hashing functions etc. but
VC++.NET won't accept it - the following code doesn't compile:

#include <hash_map>
#include <string>
std::hash_map<std::string, int> h;
int main() { h["bob"] = 1; }

The specific error refers to a file in the STL implementation which attempts
to C-style cast the string (which is now a template parameter) to a size_t:

c:\Program Files\Microsoft Visual Studio .NET\Vc7\include\xhash(38): error
C2440: 'type cast' : cannot convert from 'const std::string' to 'size_t'

Is this a bug in the VC++ .NET STL implementation, or do I need to define a
hashing function? If so, can anyone demonstrate how to do this? Thanks,

Richard

Brian Muth

2005-02-14, 9:11 pm

Are you sure it isn't stdext::hash_map<std::string, int> h; ?

Brian

"Richard Asbury" <Richard Asbury@discussions.microsoft.com> wrote in message
news:37F515D7-F043-4B46-BD24-260EDB003C81@microsoft.com...
> I'm trying to use a std::string as a key to a std::hash_map - I would've
> thought that this is possible without defining any hashing functions etc.

but
> VC++.NET won't accept it - the following code doesn't compile:
>
> #include <hash_map>
> #include <string>
> std::hash_map<std::string, int> h;
> int main() { h["bob"] = 1; }
>
> The specific error refers to a file in the STL implementation which

attempts
> to C-style cast the string (which is now a template parameter) to a

size_t:
>
> c:\Program Files\Microsoft Visual Studio .NET\Vc7\include\xhash(38): error
> C2440: 'type cast' : cannot convert from 'const std::string' to 'size_t'
>
> Is this a bug in the VC++ .NET STL implementation, or do I need to define

a
> hashing function? If so, can anyone demonstrate how to do this? Thanks,
>
> Richard
>



Tom Widmer

2005-02-14, 9:11 pm

Richard Asbury wrote:
> I'm trying to use a std::string as a key to a std::hash_map - I would've
> thought that this is possible without defining any hashing functions etc. but
> VC++.NET won't accept it - the following code doesn't compile:
>
> #include <hash_map>
> #include <string>
> std::hash_map<std::string, int> h;
> int main() { h["bob"] = 1; }
>
> The specific error refers to a file in the STL implementation which attempts
> to C-style cast the string (which is now a template parameter) to a size_t:
>
> c:\Program Files\Microsoft Visual Studio .NET\Vc7\include\xhash(38): error
> C2440: 'type cast' : cannot convert from 'const std::string' to 'size_t'
>
> Is this a bug in the VC++ .NET STL implementation, or do I need to define a
> hashing function? If so, can anyone demonstrate how to do this? Thanks,


With the version you've got, you need to define a hashing function.
Something like:

struct string_hash {
const size_t bucket_size = 4;
const size_t min_buckets = 8;
size_t operator()(const std::string& Key) const
{
size_t hash = 5381;
int c;
for (std::string::const_iterator c = Key.begin(),
end = Key.end(); c != end; ++c)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

return hash;
}

bool operator()(const std::string& keyval1,
const std::string& keyval2) const
{
return keyval1 < keyval2;
}
};

and use that for the 3rd template arg.

Tom
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com