Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are erasing elements in the vector while at the same time iterating over it. So when erasing an element, you always jump over the next element, since you increase <code>i</code> while having just shortened the vector at <code>i</code> (it would be even worse, had you used a proper iterator loop instead of an index loop). The best way to do this would be to seperate both opretions, first <em>"marking"</em> (or rather reordering) the elements for removal and then erasing them from the vector.</p> <p>This is in practice best done using the <a href="http://en.wikipedia.org/wiki/Erase-remove_idiom" rel="nofollow">erase-remove idiom</a> (<code>vector.erarse(std::remove(...), vector.end())</code>), which first uses <a href="http://en.cppreference.com/w/cpp/algorithm/remove" rel="nofollow"><code>std::remove(_if)</code></a> to reorganize the data with the non-removed elements at the beginning and returns the new end of the range, which can then be used to really delete those removed elements from the range (effectively just shortening the whole vector), using <a href="http://en.cppreference.com/w/cpp/container/vector/erase" rel="nofollow"><code>std::vector::erase</code></a>. Using a C++11 lambda, the removal condition can be expressed quite easily:</p> <pre><code>vector1.erase(std::remove_if( //erase range starting here vector1.begin(), vector1.end(), //iterate over whole vector [&amp;vector2](unsigned int i) //call this for each element { return vector2.at(i).var; }), //return true to remove vector1.end()); //erase up to old end </code></pre> <p><strong>EDIT:</strong> And by the way, as always be sure if you really need <code>std::vector::at</code> instead of just <code>[]</code> and keep in mind the implications of both (in particular the overhead of the former and <em>"maybe insecurity"</em> of the latter).</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. VO
      singulars
      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