Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to replace <key, value> in a std multimap
    primarykey
    data
    text
    <p>Rehrasing the question - Question rephrased - I have a requirement where I need to replace a pair with a new key and value. Consider this - </p> <pre><code>#include &lt;map&gt; #include &lt;string&gt; #include &lt;iostream&gt; using namespace std; int main() { std::multimap&lt;unsigned int, std::string&gt; mymap; mymap.insert(std::multimap&lt;unsigned int, std::string&gt;::value_type(0, "A1")); mymap.insert(std::multimap&lt;unsigned int, std::string&gt;::value_type(0, "A2")); mymap.insert(std::multimap&lt;unsigned int, std::string&gt;::value_type(2, "C1")); mymap.insert(std::multimap&lt;unsigned int, std::string&gt;::value_type(2, "C2")); mymap.insert(std::multimap&lt;unsigned int, std::string&gt;::value_type(1, "B1")); mymap.insert(std::multimap&lt;unsigned int, std::string&gt;::value_type(1, "B2")); mymap.insert(std::multimap&lt;unsigned int, std::string&gt;::value_type(1, "B3")); std::pair&lt;std::multimap&lt;unsigned int, std::string&gt;::iterator, std::multimap&lt;unsigned int, std::string&gt;::iterator&gt; pr = mymap.equal_range(1); std::multimap&lt;unsigned int, std::string&gt;::iterator it; for (it=pr.first; it!=pr.second; ++it) { unsigned int key = it-&gt;first; key = key+10; std::string val = it-&gt;second; val = "X" + val; mymap.erase(it); mymap.insert(std::multimap&lt;unsigned int, std::string&gt;::value_type(key, val)); } for ( it=mymap.begin() ; it != mymap.end(); it++ ) { cout &lt;&lt; (*it).first &lt;&lt; " =&gt; " &lt;&lt; (*it).second &lt;&lt; endl; } return 0; } </code></pre> <p>The program crashes on visual studio 2008 since the iterator is invalidated.</p> <p>I expect it to be:</p> <pre><code>0 =&gt; A1 0 =&gt; A2 2 =&gt; C1 2 =&gt; C2 11 =&gt; XB1 11 =&gt; XB2 11 =&gt; XB3 </code></pre> <p>The idea is I want to replace an existing entry in the map with a new entry.</p> <p>What am I doing wrong? Any help very much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload