I am declaring a hash map like map<char *, int> m
in my C++ program. But it was not working, so I followed the instructions from Using char* as a key in std::map
and declared my map like map<char *, int, cmp_str> m
. My program kind of looks like this
struct cmp_str
{
bool operator()(char const *a, char const *b)
{
return std::strcmp(a, b) < 0;
}
};
int main(int argc, char *argv[])
{
map<char *, int, cmp_str> m
//Reading strings from a file
while( not end of file )
{
// char *str contains the line
if(m.find(str) != m.end()) {m[str]++; }
else {m[str] = 1;}
}
}
When I execute the program, if finds all strings but first even if they are not inserted. When I tried using map<string, int> m;
and converted char *str
to std::string
it works fine. But the input file is so large, it takes lots of time when I use string. I am not sure why it finds all strings when I use char *
. Any help would be appreciated.