For the erase-remove idiom, why is the second parameter necessary which points to the end of the container? : <p>Consider the following code (taken from <a href="https://en.cppreference.com/w/cpp/algorithm/remove" rel="noreferrer">cppreference.com</a>, slightly adapted):</p>

<pre><code>#include &lt;algorithm&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;cctype&gt;

int main()
{
    std::string str1 = "     Text with some   spaces";
    str1.erase(std::remove(str1.begin(), str1.end(), ' '), str1.end());
    std::cout &lt;&lt; str1 &lt;&lt; '\n';

    return 0;
}
</code></pre>

<p>Why is the second parameter to <code>erase</code> neccessary? (I.e. <code>str1.end()</code> in this case.)</p>

<p>Why can't I just supply the iterators which are returned by <code>remove</code> to <code>erase</code>? Why do I have to tell it also about the last element of the container from which to erase?</p>

<p>The pitfall here is that you can also call <code>erase</code> without the second parameter but that produces the wrong result, obviously. </p>

<p>Are there use cases where I would not want to pass the end of the container as a second parameter to <code>erase</code>?</p>

<p>Is omitting the second parameter of <code>erase</code> for the erase-remove idiom always an error or could that be a valid thing to do?</p>
