STL之unique()去重函数

来源:互联网 发布:手机wifi拒绝接入网络 编辑:程序博客网 时间:2024/05/22 03:09

转载于:http://blog.csdn.net/tomorrowtodie/article/details/51907471

这是本鶸代码。。。。。。。

[cpp] view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<algorithm>  
  4. using namespace std;  
  5. const int N = 100000;  
  6. int a[N+5];  
  7. int b[N+5];  
  8. int main()  
  9. {  
  10.     int n;  
  11.     while (cin>>n)  
  12.     {  
  13.         for (int i = 0;i < n;++i)  
  14.         {  
  15.             scanf("%d",&a[i]);  
  16.         }  
  17.         sort(a,a+n);  
  18.         b[0] = a[0];int k = 0;  
  19.         for (int i = 1;i < n;++i)//去重  
  20.         {  
  21.             if (a[i]!=a[i-1])  
  22.             {  
  23.                 b[++k] = a[i];  
  24.             }  
  25.         }  
  26.         for (int i = 0;i <= k;++i)  
  27.         {  
  28.             printf("%d ",b[i]);  
  29.         }  
  30.         puts("");  
  31.     }  
  32.     return 0;  
  33. }  
然而大神是这样写的:

[cpp] view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<algorithm>  
  4. using namespace std;  
  5. const int N = 100000;  
  6. int a[N+5];  
  7. int main()  
  8. {  
  9.     int n;  
  10.     while (cin>>n)  
  11.     {  
  12.         for (int i = 0;i < n;++i)  
  13.         {  
  14.             scanf("%d",&a[i]);  
  15.         }  
  16.         sort(a,a+n);  
  17.         n = unique(a,a+n) - a;//关键的一句  
  18.         for (int i = 0;i < n;++i)  
  19.         {  
  20.             printf("%d ",a[i]);  
  21.         }  
  22.         puts("");  
  23.     }  
  24.     return 0;  
  25. }  
unique()是C++标准库函数里面的函数,其功能是去除相邻的重复元素(只保留一个),所以使用前需要对数组进行排序

上面的一个使用中已经给出该函数的一个使用方法,对于长度为n数组a,unique(a,a+n) - a返回的是去重后的数组长度

那它是怎么实现去重的呢?删除?

不是,它并没有将重复的元素删除,而是把重复的元素放到数组的最后面藏起来了

当把原长度的数组整个输出来就会发现:

[cpp] view plain copy
  1. while (cin>>n)  
  2.     {  
  3.         for (int i = 0;i < n;++i)  
  4.         {  
  5.             scanf("%d",&a[i]);  
  6.         }  
  7.         sort(a,a+n);  
  8.         int k = unique(a,a+n) - a;  
  9.         for (int i = 0;i < n;++i)  
  10.         {  
  11.             printf("%d ",a[i]);  
  12.         }  
  13.         puts("");  
  14.     }  
上述代码就是去重后再把原数组输出,测试一下看看结果就懂了






其中 1 2 8 9 10就是去重后的数组,我这里把后面“藏起来”的数也输出了,方便理解

另外,这个函数还可以这样用:

[cpp] view plain copy
  1. #include<cstdio>  
  2. #include<algorithm>  
  3. #include<vector>  
  4. using namespace std;  
  5. const int N = 1000;  
  6. int a[N + 5];  
  7. int main()  
  8. {  
  9.     int n;  
  10.     while (cin >> n)  
  11.     {  
  12.         for (int i = 0;i < n;++i) scanf("%d",&a[i]);  
  13.         sort (a, a + n);  
  14.         vector<int>v (a, a + n);  
  15.         vector<int>::iterator it = unique (v.begin(), v.end() );  
  16.         v.erase (it, v.end() );//这里就是把后面藏起来的重复元素删除了  
  17.         for ( it = v.begin() ; it != v.end() ; it++ )  
  18.         {  
  19.             printf ("%d ", *it);  
  20.         }  
  21.         puts("");  
  22.     }  
  23.     return 0;  
  24. }  




这个就是利用vector把后面藏着的元素删除了


原创粉丝点击