Can't make a hash_map with std::string for key
-
How can I make a hash map with an std::string for a key? VC++ .NET barfs, complaining that it cannot convert from type 'std::string' to 'size_t'. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador? James Merrill
-
How can I make a hash map with an std::string for a key? VC++ .NET barfs, complaining that it cannot convert from type 'std::string' to 'size_t'. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador? James Merrill
I don't know how to do this but you need to write a hash function. John
-
How can I make a hash map with an std::string for a key? VC++ .NET barfs, complaining that it cannot convert from type 'std::string' to 'size_t'. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador? James Merrill
I found this on a web search:
#include <iostream>
#include <string>
#include <hash_map>
using namespace std;/** BEGIN FIX **/
namespace std
{
template<> struct hash< std::string >
{
size_t operator()( const std::string& x ) const
{
return hash< const char* >()( x.c_str() );
}
};
}
/** END FIX **/hash_map <string, int> months;
int main(void) {
months["january"] = 31;
months["february"] = 28;
return 0;
}John
-
How can I make a hash map with an std::string for a key? VC++ .NET barfs, complaining that it cannot convert from type 'std::string' to 'size_t'. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador? James Merrill
Here's the hash function I've been using:
namespace std {
template<>
size_t hash_compare<string>::operator()(const string &s) const
{
unsigned long retv = 0;
for (string::const_iterator iter = s.begin();
iter != s.end(); ++iter)
{
retv = 5 * retv + *iter;
}
return size_t(retv);
}
}There's probably a better way to do it, though. :) - Mike
-
How can I make a hash map with an std::string for a key? VC++ .NET barfs, complaining that it cannot convert from type 'std::string' to 'size_t'. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador? James Merrill
The other ways, of course, to get working
hash_map
s would be to either- use STLPort (where
hash_map
andhash_set
have a hash comparison function defined forstd::string
s - and also you get nice features like debug mode, which tells you when you've made boo-boos like pushing an iterator outside a container, or used mismatched iterators) - Upgrade to VS.NET 2003. It cost me GBP19 (USD30 roughly) and was worth every penny - especially for the way more compliant compiler and faster IDE (well, I think it is - subjectively).
Of course, the solution in the other posts is rather more immediate :-D Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
- use STLPort (where
-
How can I make a hash map with an std::string for a key? VC++ .NET barfs, complaining that it cannot convert from type 'std::string' to 'size_t'. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador? James Merrill
Here's how Java get a hash code for the string. The algorithm should be fine: /** * Returns a hash code for this string. The hash code for a *
String
object is computed as *\* s\[0\]\*31^(n-1) + s\[1\]\*31^(n-2) + ... + s\[n-1\] \*
* using
int
arithmetic, wheres[i]
is the * _i_th character of the string,n
is the length of * the string, and^
indicates exponentiation. * (The hash value of the empty string is zero.) * * @return a hash code value for this object. */ Best regards, Alexandru Savescu P.S. Interested in art? Visit this! -
How can I make a hash map with an std::string for a key? VC++ .NET barfs, complaining that it cannot convert from type 'std::string' to 'size_t'. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador? James Merrill
Thanks to everyone who helped explain this. Thanks especially for the code fragments. Why couldn't Science, in the long run, serve As well as one's uncleared lunch-table or Mme X en Culottes de Matador? James Merrill