Word count program in C++
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/6103927/count-the-number-of-times-each-word-occurs-in-a-file">Count the number of times each word occurs in a file</a>  </p>
</blockquote>



<p>Hi,<br>
Its been a long time since I did my C++ programming.<br>
This could be quite a dumb question.<br>
I have found several programs on word count in this site.<br>
But most of them are using <code>std::string</code> as their keys.<br>
In my case, I needed to use <code>char*</code> as my key.<br>
But it seems since each <code>char*</code> has different address values, the duplicate keys are getting inserted in my map.</p>

<pre><code>char str[] = "This This";
typedef std::map&lt;char*, int&gt; 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 &lt;&lt; "Key: " &lt;&lt; itr-&gt;first  &lt;&lt; ", Value: " &lt;&lt; itr-&gt;second &lt;&lt; std::endl;
    }

    getchar();
}
</code></pre>

<p>The output I m getting for this program is</p>

<blockquote>
  <p>Key: This, Value: 1<br>
  Key: This, Value: 1</p>
</blockquote>

<p>I wanted output like</p>

<blockquote>
  <p>Key: This, Value: 2</p>
</blockquote>

<p>Can somebody tell me where did I miss what?</p>

<p>Thanks.</p>
