Note that there are some explanatory texts on larger screens.

plurals
  1. POManual control of iterator when erasing (and deleting) is necessary
    primarykey
    data
    text
    <p>In a particle system, once particles become old enough, they need to die. Since they are stored in an <code>std::vector</code> the approach that works rather well in XCode is:</p> <pre><code>for(std::vector&lt;Particle*&gt;::reverse_iterator iter = particles.rbegin(); iter != particles.rend(); ++iter) { (*iter)-&gt;update(); if ( (*iter)-&gt;isDead() ) { delete (*iter); particles.erase( --iter.base() ); } } </code></pre> <p>After booting into Windows, and compiling in Visual Studio 2010, I found that it doesn't work: see <a href="https://stackoverflow.com/questions/404258/how-do-i-erase-a-reverse-iterator-from-an-stl-data-structure">here</a>. As the answer itself states, this does not work for associative containers. What I find most frustrating here is that <code>std::reverse_iterator</code> and <code>std::iterator</code> behave differently:</p> <ul> <li><code>.erase</code> does not take a <code>reverse_iterator</code> and wants a real thing (e.g. see <a href="https://stackoverflow.com/questions/8621426/how-do-you-erase-and-continue-using-a-stdreverse-iterator">this</a>)</li> <li><code>rev_it.base()</code> call needs to be decremented in the erase call </li> <li>after erasing, I need to convert the <code>std::iterator</code> to an <code>std::reverse_iterator</code></li> </ul> <p>I thought of using a forward <code>std::iterator</code> but iterating backwards, which <a href="https://stackoverflow.com/questions/1853358/use-a-regular-iterator-to-iterate-backwards-or-struggle-with-reverse-iterator">is a terrible idea</a> - but the very need for iterating backwards, really, is to ensure that the loop doesn't skip neighboring members of erased <code>particles</code>.</p> <p>What makes sense to me, though, is <em>not iterating if the <code>.erase()</code> call is made</em>:</p> <pre><code>for( std::vector&lt;Particle*&gt;::iterator iter = particles.begin(); iter != particles.end(); ) { (*iter)-&gt;update(); if ( (*iter)-&gt;isDead() ) { delete (*iter); iter = particles.erase(iter); } else { ++iter; } } </code></pre> <p>This compiles, works, and doesn't seem to be a problem. The question, though, is:</p> <p><strong>Am I overlooking something that renders this a particularly stupid idea?</strong></p> <p>(I'm sure that <code>iter</code> will point at the correct next value by taking advantage of the <code>.erase()</code> function's <code>return</code> value, and it seems more readable to me than <code>--iter.base()</code> call.)</p> <p>That parenthetical aside, a Russian saying that comes to mind is <em>"One who's been burned on hot milk blows on cold water."</em> </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.
 

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