c++ 查重+排序函数

输入

第一行n。第二行有n个元素。

输出

 

查重排序后的元素

 

样例:

输入:

5

1 1 2 3 4

输出:

1 2 3 4

unique的作用是“去掉”容器中相邻元素的重复元素

注意:用unique只能对有序的数组进行查重,所以要先进行排序

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
  sort(a,a+n);
int t=unique(a,a+n)-a; for(int i=0;i<t;i++) cout<<a[i]<<' '; return 0; }
posted @ 2020-01-04 11:10  lipu123  阅读(1069)  评论(0)    收藏  举报