Namespaces
Variants
Views
Actions

Talk:cpp/algorithm/unique

From cppreference.com
< Talk:cpp‎ | algorithm
Revision as of 01:58, 11 February 2017 by 80.147.162.235 (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 <algorithm>
   #include <cstdlib>
   #include <iostream>
   #include <iterator>
   #include <vector>
   
   int main()
   {
       const auto p = [](const int lhs, const int rhs) { return std::abs(lhs - rhs) <= 1; };
   
       std::vector<int> 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<int>(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)