May 31, 2011 at 11:18am UTC
My code is like this:
#include <iostream>
#include <algorithm>
#include <hash_map>
#include <string>
struct str_hash{
std::size_t operator()(const std::string& str) const
{
unsigned long __h = 0;
for (std::size_t i = 0 ; i < str.size() ; i ++)
__h = 5*__h + str[i];
return std::size_t(__h);
}
};
struct compare_str{
bool operator()(const char* p1, const char*p2) const{
return strcmp(p1,p2)==0;
}
};
typedef stdext::hash_map<const char*, std::string, str_hash, compare_str> StrIntMap;
//If I don't add the next line code . the program can complied successfully.
//but I can't complied when I add the code: StrIntMap hash_mapme;
StrIntMap hash_mapme;
I complied this code on VS 2005,
What will I do if I want to use hash_map? please give me an example please.
thanks .
May 31, 2011 at 2:30pm UTC
Please edit your post and put your code inside code tags.
Jun 1, 2011 at 1:21am UTC
sorry,this will be ok.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <iostream>
#include <algorithm>
#include <hash_map>
#include <string>
struct str_hash{
std::size_t operator ()(const std::string& str) const
{
unsigned long __h = 0;
for (std::size_t i = 0 ; i < str.size() ; i ++)
__h = 5*__h + str[i];
return std::size_t(__h);
}
};
struct compare_str{
bool operator ()(const char * p1, const char *p2) const {
return strcmp(p1,p2)==0;
}
};
typedef stdext::hash_map<const char *, std::string, str_hash, compare_str> StrIntMap;
//If I don't add the next line code . the program can complied successfully.
//but I can't complied when I add the code: StrIntMap hash_mapme;
StrIntMap hash_mapme;
Last edited on Jun 1, 2011 at 1:22am UTC
Jun 1, 2011 at 8:38am UTC
And the compiler error is...
hash_map<Key, Data, HashFcn, EqualKey, Alloc>
Just a guess, but ¿shouldn't the HashFcn apply to the key?
In your case const char *