Swap two elements of map
<p>I have a <code>map&lt;int, Button*&gt;</code> wherein the button class has several attributes, in particular a integer variable named position. </p>

<p>If I want to swap two positions in Button class, I have to change the key, to be always key = Button-> position and it has to be a map.</p>

<p>I thought of deleting the two positions of the map (using the erase) and reinsert (indicating the index):</p>

<p>Example (indexFirst and indexSecond are known):</p>

<pre><code>map&lt;int, Button*&gt; buttons;

int posOfFirst = buttons.find(indexFirst)-&gt;second-&gt;getPos();
int posOfSecond = buttons.find(indexSecond)-&gt;second-&gt;getPos();

Button* button1 = buttons.find(indexFirst)-&gt;second;
Button* button2 = buttons.find(indexSecond)-&gt;second;

buttons.erase(indexFirst);
buttons.erase(indexFirst);

buttons[posOfSecond] = button2;
buttons[posOfFirst] = button1;
</code></pre>

<p>But appears not to change the object. Why?</p>
