Can any one explain the following behavior of the std::remove algorithm ( which I find to be pretty unusual) :
here's a little program to demonstrate the issue
std::vector<int> Vec;
Vec.push_back(3);
Vec.push_back(7);
Vec.push_back(9);
Vec.push_back(6);
Vec.push_back(1);
Vec.push_back(6);
Vec.push_back(2);
std::vector<int>::iterator iter;
std::remove(Vec.begin(), Vec.end(), 6);
for (int i : Vec)
{
std::cout << i;
}
now this is an excrept from http://www.cplusplus.com/reference/algorithm/remove/
"....The removal is done by replacing the elements that compare equal to val by the next element that does not, and signaling the new size of the shortened range by returning an iterator to the element that should be considered its new past-the-end element."
elementS, as in plural.
now, I understand what the basic idea is - I'm not asking you to explain that. The algorithm should find all elements for which the comparison with the value passed as the third argument equals true, put them all into the back of the container and return an iterator to the new logical end of the container - the start of the sequence where the "removed" elements have been placed. As far as I understand, the next time you iterate through the container, the elements in that sequence should be ignored.
What I don't understand is, how then, the program I used here as an example has the following output:
3 7 9 1 2 6 2
Even more peculiar to me is the fact that if you add another entry with a value other than 6 between the 2nd 6 and the 2 at the end, the output no longer contains a 6.
Now, I am aware of the member erase/std::remove combo you can use to remove multiple elements from a range. I tried it on this code and (to my surprise, given the context) it produces the results you'd expect it to.
So, I'm not asking for a way to to remove multiple occurences of the same value from a container, I'm asking if anyone can explain just what in the world is going under the hood of this algorithm?