Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a reason to use `remove` outside of the erase-remove idiom?
    primarykey
    data
    text
    <p>As far as the algorithm is concerned, removing a set of element from a contiguous array can be done effectively in two parts.</p> <ol> <li>Move all the elements not to be deleted to the front of the array.</li> <li>Mark the array smaller.</li> </ol> <p>This can be done in <code>C++</code> with the erase-remove idiom.</p> <pre><code>vector&lt;T&gt; v; // v = {0,1,2,3,0,0,7}; vector&lt;T&gt;::iterator it = remove(v.begin(),v.end(),e); // move all elements not to be deleted to the front // Yes, remove is not the brightest name for that. // Especially as list::remove really remove elements from the list. // now v = {1,2,3,7,?,?,?} results marked in question marks // are implementation dependent. v.erase(it,v.end()); // get rid of the elements marked as question marks. // v = {1,2,3,7} </code></pre> <p>Now, the content of the elements in the question mark is unknown. The only thing we can do with them is get rid of them (by overwriting them, or by removing them).</p> <p>Is there a real world situation where you need to use <code>remove</code> without erase? The only situation I could think of is</p> <pre><code>copy(src.begin(),src.end(),remove(v.begin(),v.end(),e),v.end()); </code></pre> <p>replacing all <code>A</code>s with <code>B</code>s, and requiring that all those new <code>B</code>s will be contiguous. Doesn't make too much sense.</p> <p><strong>edit</strong>: Does it make sense to anything other than contigious memory container (<code>deque</code> and <code>vector</code> actually)?</p> <p>If indeed I'm correct, why was it implemented as a standalone algorithm, instead of <code>vector::remove_if</code>, <code>dequeue::remove_if</code> etc.</p>
    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.
 

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