Count how many times each distinct word appears in its input C++
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;map&gt;

int main()
{
    std::string input;
    std::cout &lt;&lt; "Enter input: ";
    std::getline(std::cin, input);

    std::map&lt;std::string, int&gt; m;
    std::map&lt;std::string, int&gt;::iterator it;
    std::istringstream iss(input);
    std::string words;

    do {
        iss &gt;&gt; words;
        it = m.find(words);
        if(it != m.end())
        {
            m.insert(it, std::pair&lt;std::string, int&gt;(words, m[words] + 1));
        }
        else
        {
            m.insert(std::pair&lt;std::string, int&gt;(words, 1));
        }
    }while(iss);

    for(std::map&lt;std::string, int&gt;::iterator it = m.begin(); it != m.end(); ++it)
    {
        std::cout &lt;&lt; it-&gt;first &lt;&lt; " - " &lt;&lt; it-&gt;second &lt;&lt; std::endl;
    }
    return 0;
}
</code></pre>

<p>The problem is that it prints 1 for every word, even if it appears twice. What could be the problem? I'm not sure if my test for an iterator not to be empty is correct.</p>
