Advertisement
Brick

Find unique elements in range

Nov 2nd, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. template <typename InputIterator, typename UnaryFunction, typename Compare = std::equal_to<void>>
  2. void uniques(InputIterator begin, InputIterator end, UnaryFunction f, Compare comp = Compare())
  3. {
  4.     while (begin != end)
  5.     {
  6.         InputIterator next = std::next(begin), find = std::find_if(next, end, [&] (auto& value)
  7.         {
  8.             return !comp(*begin, value);
  9.         });
  10.  
  11.         if (next == find)
  12.         {
  13.             f(*begin);
  14.         }
  15.  
  16.         begin = find;
  17.     }
  18. }
  19.  
  20. template <typename InputIterator1, typename InputIterator2, typename BinaryFunction, typename Compare = std::less<void>>
  21. void intersect(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2, InputIterator2 end2, BinaryFunction f, Compare comp = Compare())
  22. {
  23.     while (begin1 != end1 && begin2 != end2)
  24.     {
  25.         if (comp(*begin1, *begin2))
  26.         {
  27.             ++begin1;
  28.         }
  29.         else if (comp(*begin2, *begin1))
  30.         {
  31.             ++begin2;
  32.         }
  33.         else
  34.         {
  35.             f(*begin1++, *begin2++);
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement