Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With all due respect to Fred's hard work, let me add this: the <code>move_if</code> is no different than <code>remove_copy_if</code> at an abstract level. The only implementation level change is the <code>end()</code> iterator. You are still not getting any <code>erase()</code>. <strong>The accepted answer does not <code>erase()</code> the matched elements -- part of the OP's problem statement.</strong></p> <p><strong>As for the OP's question: what you want is an in-place splice. This is possible for lists. However, with <code>vectors</code> this will not work. Read about when and how and why iterators are invalidated. You will have to take a two pass algorithm.</strong></p> <blockquote> <p><code>remove_copy_if</code> and other algorithms can only reorder the elements in the container,</p> </blockquote> <p>From SGI's documentation on <a href="http://www.sgi.com/tech/stl/remove_copy_if.html" rel="nofollow noreferrer"><code>remove_copy_if</code></a>:</p> <blockquote> <p>This operation is stable, meaning that the relative order of the elements that are copied is the same as in the range [first, last). </p> </blockquote> <p>So no relative reordering takes place. Moreover, this is a copy, which means the elements from <code>Source</code> <code>vector</code> in your case, is being copied to the <code>Container vector</code>.</p> <blockquote> <p>how can I call erase on the source container?</p> </blockquote> <p>You need to use a different algorithm, called <a href="http://www.sgi.com/tech/stl/remove_if.html" rel="nofollow noreferrer"><code>remove_if</code></a>:</p> <blockquote> <p><code>remove_if</code> removes from the range <code>[first, last)</code> every element <code>x</code> such that <code>pred(x)</code> is true. That is, <code>remove_if</code> returns an iterator <code>new_last</code> such that the range <code>[first, new_last)</code> contains no elements for which <code>pred</code> is true. The iterators in the range <code>[new_last, last)</code> are all still dereferenceable, but the elements that they point to are unspecified. <code>Remove_if</code> is stable, meaning that the relative order of elements that are not removed is unchanged. </p> </blockquote> <p>So, just change that <code>remove_copy_if</code> call to:</p> <pre><code>vector&lt;string&gt;::iterator new_last = remove_if(Strings.begin(), Strings.end(), Pred); </code></pre> <p>and you're all set. Just keep in mind, your <code>Strings vector</code>'s range is no longer that defined by the iterators <code>[first(), end())</code> but rather by <code>[first(), new_last)</code>.</p> <p>You can, if you want to, remove the remaining <code>[new_last, end())</code> by the following:</p> <pre><code>Strings.erase(new_last, Strings.end()); </code></pre> <p>Now, your <code>vector</code> has been shortened and your <code>end()</code> and <code>new_last</code> are the same (one past the last element), so you can use as always:</p> <pre><code>copy(Strings.begin(), Strings.end(), ostream_iterator(cout, "\")); </code></pre> <p>to get a print of the strings on your console (<code>stdout</code>).</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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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