Talk:cpp/algorithm/unique

For versions 3 and 4, it appears to me the documentation is unclear if the binary predicate is not transitive.

Consider the following program:

#include #include #include #include #include int main {       const auto p = [](const int lhs, const int rhs) { return std::abs(lhs - rhs) <= 1; }; std::vector v{0, 1, 2, 4, 6, 7, 6, 5, 9, 10}; v.erase(std::unique(v.begin, v.end, p), v.end); std::copy(v.cbegin, v.cend, std::ostream_iterator (std::cout, " ")); std::cout << '\n'; return 0; }

Is it guaranteed to print the following? 0 2 4 6 9

It would be great if it could be clarified, maybe the standard mandates a certain behavior, maybe in this case it's undefined, but either way it would be useful information! 80.147.162.235 01:58, 11 February 2017 (PST)


 * You probably already saw it, but his has been fixed by an edit from T. Canens. An equivalence relation R is reflexive (R(a,a)), symmetrical (if R(a,b), then R(b,a)) and transitive (if R(a,b) and R(b,c), then R(a,c)). Arvo (talk) 06:49, 12 October 2017 (PDT)
 * indeed, and also there is now a Stackoverflow discussion that points out that the clever use of std::unique to drop whitespace violates that rule. --Cubbi (talk) 06:56, 12 October 2017 (PDT)

UB in example
The removal of consecutive spaces example exhibits undefined behavior, as was reported by user Andre on stackoverflow. It will still work for either possible implementation that is listed, but it violates the requirement "The comparison function shall be an equivalence relation." [alg.unique/2]. There is no elegant, &lt;algorithm>-based fix that I can think of. Arvo (talk) 07:02, 12 October 2017 (PDT)