std::unique specification vs. reality
I noticed the other day at work that std::unique(It,It,Pred) is
specified to remove each element of the sequence where the return of
pred(*i, *(i - 1)) is false, leaving only the first element in the
non-unique sub-sequence.
However, both the MSVC and G++ STL's do not do this. They compare
each element against the first element of the non-unique sub-sequence,
not against the element preceding it. This changes the results (and
actually is what I want---but since it appears to be nonstandard, I
did not want to rely on that implementation).
For example (untested):
template<class T>
bool
within_threshold(T a, T b, T t)
{
return std::abs(a - b) < t;
}
std::vector<double> vec = list_of(1.0)(1.05)(1.1)(1.11);
vec.erase(
std::unique(vec.begin(), vec.end(),
boost::bind(&within_threshold<double>, _1, _2, 0.09)),
vec.end());
According to how I read the standard, vec should now contain [1.0].
But the result I get is [1.0, 1.1] (which happens to be what I
actually want).
The question: am I reading the standard wrong? Is the specification
for std::unique() implemented correctly in anyone's standard library
implementation, or does everyone implement it this way?
(This algorithm seems more useful to me than the one the standard
suggests, by the way.)
--
Jordan DeLong
fracture@allusion.net
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]