Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With the code, I understand what you're going for now. I'd do it pretty much the first way you suggest: each time through the outer loop, make a temporary copy of the <code>current_partition</code> data structure, and then work on that, discarding it at the end.</p> <p>You said that the problem with that would be that you couldn't use an iterator into the original map to find the element you ought to be working on. That's true; you can't do that directly. But it's a <em>map</em>. The element you're working on will have a <em>key</em> which will be the same in any copy of the data structure, so you can use that to create an iterator to the element that you ought to be working on in the copy.</p> <p>For instance:</p> <pre><code>std::map&lt;int,int&gt; current_partition = bitset_to_map(centre-&gt;second-&gt;bit_partitions); int num_parts = (std::max_element(current_partition.begin(), current_partition.end(),value_comparer))-&gt;second; for (std::map&lt;int,int&gt;::iterator itr = current_partition.begin(); itr != current_partition.end(); ++itr) { // Make a temporary copy of the map. Iterators between the original and the copy aren't // interchangeable, but the keys are. std::map&lt;int,int&gt; tmp_current_partition = current_partition; // Use the iterator itr to get the key for the element you're working on (itr-&gt;first), // and then use that key to get an equivalent iterator into the temporary map using find() std::map&lt;int,int&gt;::iterator tmp_itr = tmp_current_partition.find(itr-&gt;first); // Then just replace current_partition with tmp_current_partition and // itr with tmp_itr in the remainder (hopefully I didn't miss any) for (int next_part = 0; next_part&lt;num_parts+1; ++next_part) { if (next_part != tmp_itr-&gt;second) { int current_part = tmp_itr-&gt;second; tmp_itr-&gt;second = next_part; std::vector&lt;int&gt; first_changed_part, last_changed_part; for (std::map&lt;int,int&gt;::iterator new_itr = tmp_current_partition.begin(); new_itr != tmp_current_partition.end(); ++new_itr) { if (new_itr-&gt;second == current_part) first_changed_part.push_back(new_itr-&gt;first); if (new_itr-&gt;second == next_part) last_changed_part.push_back(new_itr-&gt;first); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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