C++ unordered_map insert into vector : <p>I am trying to write a program which will take list of strings as input and create hash table with string name and its position.</p>

<p>Example:<br>
vector words {"first", "second", "third", "forth", "second"};</p>

<p>output:<br>
first 1<br>
second 2,5<br>
third 3<br>
forth 4  </p>

<p>I am facing two problems please find them in code comment below.<br>
Please tell me what am i doing wrong?</p>

<pre><code>int main()
{
    vector&lt;string&gt; words {"first", "second", "third", "forth", "second"};

    unordered_map&lt;string, vector&lt;int&gt;&gt; hash_table;
    unordered_map&lt;string, vector&lt;int&gt;&gt;::const_iterator hash_it;

    int loc = 1;

    for(auto n = words.begin(); n != words.end(); ++n){
        hash_it = hash_table.find(*n);
        if(hash_it == hash_table.end())
            hash_table.insert(make_pair(*n, vector&lt;int&gt; (loc)));
        else
            //hash_it-&gt;second.push_back(loc);   //Problem 1 - this statement gives error

        ++loc;
    }

    for(auto&amp; n:hash_table){
        cout&lt;&lt;"Word - "&lt;&lt;n.first&lt;&lt;" Loc -";
        vector&lt;int&gt; tmp1 = n.second;
        for(auto j = tmp1.begin(); j != tmp1.end(); ++j)
            cout&lt;&lt;" "&lt;&lt;*j;
        cout&lt;&lt;endl;
    }
}
</code></pre>

<p>Problem 2 - location values are 0<br>
Output of program -<br>
Word - forth Loc - 0<br>
Word - third Loc - 0<br>
Word - second Loc - 0<br>
Word - first Loc - 0  </p>
