Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are erasing the same element (at indexFirst) twice (look at your code). Also it appears that you are inserting the elements at the same positions as their were initially:</p> <pre><code>buttons[posOfSecond] = button2; buttons[posOfFirst] = button1; </code></pre> <p>I thing should be changed to :</p> <pre><code>buttons[pos1] = button2; buttons[pos2] = button1; </code></pre> <p>I also would recommend a better strategy. Instead of juggling with removals and insertions, make a mutator method in your Button class, which allows you to set the value of the position attribute. You then just acquire the position of both Buttons (as you did in the first part of your code using the accessor methods) and then you assign the first position to the second button and the second position to the first button. You should have something like this in your Button header:</p> <pre><code>void setPos(int pos); </code></pre> <p>So here is an example:</p> <pre><code>map&lt;int, Button*&gt; buttons; //Find the buttons only once and save their references //if you need further information that //their storing, instead of constantly searching //through the map. This is more efficient Button* button1 = buttons.find(indexFirst)-&gt;second; Button* button2 = buttons.find(indexSecond)-&gt;second; int pos1 = button1-&gt;getPos(); int pos2 = button2-&gt;getPos(); button1-&gt;setPos(pos2); button2-&gt;setPos(pos1); buttons[pos2] = button1; buttons[pos1] = button2; </code></pre> <p>And you are done.</p> <p>This will be true if the only unique data that the buttons are storing is their position, otherwise you have to swap other information too.</p> <p>There are a lot of strategies here, with different trade ofs, but make sure you always take into account not only if it is working, but whether it is efficient.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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