Possible Duplicate:
Count the number of times each word occurs in a file
Hi,
Its been a long time since I did my C++ programming.
This could be quite a dumb question.
I have found several programs on word count in this site.
But most of them are using std::string
as their keys.
In my case, I needed to use char*
as my key.
But it seems since each char*
has different address values, the duplicate keys are getting inserted in my map.
char str[] = "This This";
typedef std::map<char*, int> word_count_t;
typedef word_count_t::iterator word_count_iter_t;
int _tmain(int argc, _TCHAR* argv[])
{
char *token = strtok(str, " ");
word_count_t word_count;
word_count_iter_t itr = NULL;
while(token) {
++word_count[token];
token = strtok(NULL, " ");
}
for(itr = word_count.begin(); itr != word_count.end(); itr++) {
std::cout << "Key: " << itr->first << ", Value: " << itr->second << std::endl;
}
getchar();
}
The output I m getting for this program is
Key: This, Value: 1
Key: This, Value: 1
I wanted output like
Key: This, Value: 2
Can somebody tell me where did I miss what?
Thanks.