I don't have it here but I will check. I would be a surprise: saying
that std::remove doesn't remove would mean it is a misnommer. Perhaps
the distinction is a bit subtle and another world should have been used
but it doesn't mean it is false.
It is your understanding of 'remove' that is at fault. See defintion of
remove at SGI STL.
http://www.sgi.com/tech/stl/remove.html
yes, that is much better explanation
I checked the NOTES:
NOTES >>> The meaning of "removal" is somewhat subtle. Remove does
NOTES >>> not destroy any iterators, and does not change the distance
NOTES >>> between first and last. (There's no way that it could do
NOTES >>> anything of thesort.) So, for example, if V is a vector,
NOTES >>> remove(V.begin(), V.end(), 0)does not change V.size(): V will
NOTES >>> contain just as many elements as it did before. Remove returns
NOTES >>> an iterator that points to the end of the resulting range after
NOTES >>> elements have been removed from it; it follows that the
NOTES >>> elements after that iterator are of no interest, and may be
NOTES >>> discarded.
e.g.
copy(V.begin(), V.end(), ostream_iterator<int>(cout, " "));
// The output is "3 1 4 1 5 9".
vector<int>::iterator new_end = remove(V.begin(), V.end(), 1);
copy(V.begin(), new_end, ostream_iterator<int>(cout, " "));
// The output is "3 4 5 9".
Therefore I understand that V.size() is still same but we have this range
of V.begin() & V.end() has got new iterator at say V.end_last() that says:
In iterator range ( V.begin(), V.end_last() ) contains elements of V
where integer 1 is not present (removed)
integer 1 is present In iterator range ( V.end_last(), V.end() )
Am I right ?
I think NO, I am wrong because this example says something else:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
std::vector<int> V;
V.push_back(3);
V.push_back(1);
V.push_back(4);
V.push_back(1);
V.push_back(5);
V.push_back(9);
std::copy(V.begin(), V.end(), std:
stream_iterator<int>(std::cout, " "));
// The output is "3 1 4 1 5 9".
std::vector<int>::iterator new_end = remove(V.begin(), V.end(), 1);
std::cout << "\n--------------- NEW RANGE -----------\n";
std::copy(V.begin(), new_end, std:
stream_iterator<int>(std::cout, " "));
// The output is "3 4 5 9".
std::cout << "\n-------------- REMOVED RANGE -------------\n";
std::copy( new_end, V.end(), std:
stream_iterator<int>( std::cout, " " ) );
std::cout << std::endl;
return 0;
}
============ OUTPUT ===================
~/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra test.cpp
~/programming/c++ $ .a/
..a/: command not found
~/programming/c++ $ ./a.out
3 1 4 1 5 9
--------------- NEW RANGE -----------
3 4 5 9
-------------- REMOVED RANGE -------------
5 9
~/programming/c++ $
so did it really erase the iterator-range ( new_end, V.end() ) ?
NOTES >>> If you are removing elements from a Sequence, you may simply
NOTES >>> erase them. That is, a reasonable way of removingelements from
NOTES >>> a Sequence is S.erase(remove(S.begin(), S.end(), x), S.end())
I guess that is better and little Googling tells me that it is Scott
Meyer's technique # 32
-- arnuld
http://lispmachine.wordpress.com