How to Remove Duplicate Elements from Vector in C++ using std::u

  • 时间:2020-10-11 15:17:18
  • 分类:网络文摘
  • 阅读:154 次

Removing duplicate elements and only keeping the unique items in an array (or vector, list) is easy. We can use std::unique method from STL.

The std::unique (from algorithms header) has the following signature:

1
2
3
4
5
template<class ForwardIt>
ForwardIt unique(ForwardIt first, ForwardIt last);
 
template<class ForwardIt, class BinaryPredicate>
ForwardIt unique(ForwardIt first, ForwardIt last, BinaryPredicate p);
template<class ForwardIt>
ForwardIt unique(ForwardIt first, ForwardIt last);

template<class ForwardIt, class BinaryPredicate>
ForwardIt unique(ForwardIt first, ForwardIt last, BinaryPredicate p);

Example C++ code to use std::unique to remove duplicate numbers from vector

It requires the vector to be sorted. Here is an example use:

1
2
3
4
5
6
7
vector <int> arr = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5};
auto it = std::unqiue(begin(arr), end(arr));
// now arr becomes: {1, 2, 3, 4, 5, x, x, x, x, x, x}
// x is indeterminate
// then we can trim the array to make a unique
arr.erase(it, end(arr));
// x is now {1, 2, 3, 4, 5}
vector <int> arr = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5};
auto it = std::unqiue(begin(arr), end(arr));
// now arr becomes: {1, 2, 3, 4, 5, x, x, x, x, x, x}
// x is indeterminate
// then we can trim the array to make a unique
arr.erase(it, end(arr));
// x is now {1, 2, 3, 4, 5}

The std::unique function will return the iterator that is beyond the last element of the unique array – which can be used later for removing the extra duplicate elements from the C++ list/vector.

C++ Generic Implementations of std::unique using template

Here are two possible implementation of the generic std::unique method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
template<class F>
F unique(F first, F last)
{
    if (first == last) {
        return last;
    }
    F res = first;
    while (++first != last) {
        if (!(*res == *first) && ++ res != first) {
            *res = std::move(*first);
        }
    }
    return ++ res;
}
 
template<class F, class BinaryPredicate>
F unique(F  first, F last, BinaryPredicate p)
{
    if (first == last) {
        return last;
    }
    F res = first;
    while (++first != last) {
        if (!p(*res, *first) && ++res != first) {
            *result = std::move(*first);
        }
    }
    return ++res;
}
template<class F>
F unique(F first, F last)
{
    if (first == last) {
        return last;
    }
    F res = first;
    while (++first != last) {
        if (!(*res == *first) && ++ res != first) {
            *res = std::move(*first);
        }
    }
    return ++ res;
}

template<class F, class BinaryPredicate>
F unique(F  first, F last, BinaryPredicate p)
{
    if (first == last) {
        return last;
    }
    F res = first;
    while (++first != last) {
        if (!p(*res, *first) && ++res != first) {
            *result = std::move(*first);
        }
    }
    return ++res;
}

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
求甲乙两车的速度  获纪念奖的有多少人?  程大位与剩余定理  “位数”和“数位”的意义为什么不同?  节约用水的资料  求发芽率、合格率、出粉率等百分率的公式中为什么都要乘100%?  为什么公历7月和8月都是31天  引流粉丝到公众号,网站运营的一点思考,引流技巧实操  站长如何赚钱?下面七条你做到了么?  个人站长成功的七条经验分享 
评论列表
添加评论