Brick icon

Find unique elements in range

Brick | PRO | 11/03/17 04:00:51 AM UTC | 0 ⭐ | 252 👁️ | Never ⏰ | []
C++ |

1.06 KB

|

None

|

0 👍

/

0 👎

template <typename InputIterator, typename UnaryFunction, typename Compare = std::equal_to<void>>
void uniques(InputIterator begin, InputIterator end, UnaryFunction f, Compare comp = Compare())
{
    while (begin != end)
    {
        InputIterator next = std::next(begin), find = std::find_if(next, end, [&] (auto& value)
        {
            return !comp(*begin, value);
        });
 
        if (next == find)
        {
            f(*begin);
        }
 
        begin = find;
    }
}
 
template <typename InputIterator1, typename InputIterator2, typename BinaryFunction, typename Compare = std::less<void>>
void intersect(InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2, InputIterator2 end2, BinaryFunction f, Compare comp = Compare())
{
    while (begin1 != end1 && begin2 != end2)
    {
        if (comp(*begin1, *begin2))
        {
            ++begin1;
        }
        else if (comp(*begin2, *begin1))
        {
            ++begin2;
        }
        else
        {
            f(*begin1++, *begin2++);
        }
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎