erase(remove_if()) anomaly
<p>I've created a function to run through a vector of strings and remove any strings of length 3 or less.  This is a lesson in using the STL Algorithm library.</p>

<p>I'm having trouble in that the functions work but not only does it delete strings of length 3 or less but it also appends the string "vector" to the end. </p>

<p>The output should be</p>

<pre><code>This test vector
</code></pre>

<p>and instead it is</p>

<pre><code>This test vector vector"
</code></pre>

<p>How can I fix it?</p>

<pre><code>/*
* using remove_if and custom call back function, write RemoveShortWords 
* that accepts a vector&lt;string&gt; and removes all strings of length 3 or
* less from it. *shoot for 2 lines of code in functions.
*/

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;
#include &lt;vector&gt;
#include &lt;iterator&gt;
using namespace std;

bool StringLengthTest(string test) //test condition for remove_if algo.  
{
    return test.length() &lt;= 3;
}

void RemoveShortWords(vector&lt;string&gt; &amp;myVector)
{
    //erase anything in vector with length &lt;= 3
    myVector.erase(remove_if(myVector.begin(),
                             myVector.end(),
                             StringLengthTest));
}

int main ()
{
    //add some strings to vector
    vector&lt;string&gt; myVector;
    myVector.push_back("This");
    myVector.push_back("is");
    myVector.push_back("a");
    myVector.push_back("test");
    myVector.push_back("vector");

    //print out contents of myVector (debugging)
    copy(myVector.begin(), myVector.end(), ostream_iterator&lt;string&gt;(cout," "));
    cout &lt;&lt; endl; //flush the stream

    RemoveShortWords(myVector); //remove words with length &lt;= 3

    //print out myVector (debugging)
    copy(myVector.begin(), myVector.end(), ostream_iterator&lt;string&gt;(cout," "));
    cout &lt;&lt; endl;

    system("pause");
    return 0;
}
</code></pre>
