删掉重复的数据

 

一)过滤重复的数据。即把原来重复的数据过滤,只保留一份。

例如:int a[]={1,3,4,3,2,5,2};
输出:1 3 4 2 5

①实现方法一(采用STL):

  1. #include <iostream>  
  2. #include <algorithm>     
  3. #include <set>  
  4.   
  5. using namespace std;  
  6.   
  7. void Dedup(FILE* infile,FILE* outfile)  
  8. {  
  9.     int iTemp;  
  10.     unsigned int nCount = 0;  
  11.   
  12.     typedef set<int> IntSet;  
  13.     IntSet iArray;  
  14.   
  15.     while(NULL == feof(infile))  
  16.     {  
  17.         fscanf(infile,"%d",&iTemp);  
  18.         iArray.insert(iTemp);  
  19.   
  20.         nCount ++;  
  21.     }  
  22.   
  23.     printf("nCount = %d\n",nCount);//total numbers  
  24.          
  25.     for(IntSet::iterator iter = iArray.begin(); iter != iArray.end(); ++ iter)  
  26.     {  
  27.         fprintf(outfile,"%d ",*iter);  
  28.     }  
  29.   
  30. }  
  31.   
  32. int main()  
  33. {  
  34.     FILE *fpInput,*fpOutput;  
  35.   
  36.     if(NULL == (fpInput = fopen("D:\\input.txt","r")))  
  37.     {  
  38.         printf("File read error!\n");  
  39.         exit(1);  
  40.     }  
  41.   
  42.     if(NULL == (fpOutput = fopen("D:\\output.txt","w")))  
  43.     {  
  44.         printf("File write error!\n");  
  45.         exit(1);  
  46.     }  
  47.   
  48.     Dedup(fpInput,fpOutput);      
  49.     printf("\n");  
  50.   
  51.     fclose(fpInput);  
  52.     fclose(fpOutput);  
  53.   
  54.     return 0;  
  55. }  

②实现方法二(采用STL):

  1. #include <iostream>  
  2. #include <algorithm>       
  3. #include <vector>    
  4.   
  5. using namespace std;  
  6.   
  7. void Dedup(FILE* infile,FILE* outfile)  
  8. {  
  9.     int iTemp;  
  10.     unsigned int nCount = 0;  
  11.   
  12.     vector<int> iArray,iRes;  
  13.   
  14.     while(NULL == feof(infile))  
  15.     {  
  16.         fscanf(infile,"%d",&iTemp);  
  17.         iArray.push_back(iTemp);  
  18.   
  19.         nCount ++;  
  20.     }  
  21.   
  22.     printf("nCount = %d\n",nCount);//total numbers  
  23.   
  24.     sort(iArray.begin(), iArray.end());//sort   
  25.   
  26.     unique_copy(iArray.begin(), iArray.end(),back_inserter(iRes));//去掉重复的  
  27.          
  28.     for(vector <int>::const_iterator iter = iRes.begin(); iter != iRes.end(); ++ iter)  
  29.     {  
  30.         fprintf(outfile,"%d ",*iter);  
  31.     }  
  32. }  
  33. int main()  
  34. {  
  35.     FILE *fpInput,*fpOutput;  
  36.   
  37.     if(NULL == (fpInput = fopen("D:\\input.txt","r")))  
  38.     {  
  39.         printf("File read error!\n");  
  40.         exit(1);  
  41.     }  
  42.   
  43.     if(NULL == (fpOutput = fopen("D:\\output.txt","w")))  
  44.     {  
  45.         printf("File write error!\n");  
  46.         exit(1);  
  47.     }  
  48.   
  49.     Dedup(fpInput,fpOutput);  
  50.     printf("\n");  
  51.   
  52.     fclose(fpInput);  
  53.     fclose(fpOutput);  
  54.   
  55.     return 0;  
  56. }  

③实现方法三(采用STL):

  1. #include   <iostream>   
  2. #include   <string>   
  3. #include   <vector>   
  4. #include   <set>   
  5. #include   <algorithm>   
  6.   
  7. using namespace std;  
  8.   
  9. template <typename T>   
  10. void print_element(T value)   
  11. {   
  12.     cout<<value<<" ";   
  13. }   
  14.   
  15. template <typename T>   
  16. unsigned int MakeUniqueArray1(T* array, unsigned int length)   
  17. {   
  18.     set<T> s(array, array + length);   
  19.     //copy(s.begin(), s.end(), array);   
  20.   
  21.     printf("%d Number1.\n",s.size());  
  22.     return s.size();   
  23. }  
  24.   
  25. template <typename T>   
  26. unsigned int MakeUniqueArray2(T* array, unsigned int length)   
  27. {   
  28.     vector<T> v(array, array + length);   
  29.     sort(v.begin(), v.end());   
  30.   
  31.     T* end = unique_copy(v.begin(), v.end(), array);   
  32.   
  33.     printf("%d Number2.\n",end - array);  
  34.   
  35.     return end - array;   
  36. }   
  37.   
  38. int main()   
  39. {   
  40.     int a[] = {0,1,2,3,4,5,6,7,8,9,0,9,8,7,6,5,4,3,2,1};   
  41.       
  42.     unsigned int len = sizeof(a) / sizeof(int);  
  43.   
  44.     len = MakeUniqueArray1(a, len);   
  45.     len = MakeUniqueArray2(a, len);   
  46.       
  47.     void (*pfi)(int) = print_element;   
  48.     for_each(a, a + len, print_element<int>);   
  49.          
  50.     printf("\n");  
  51.     return 0;   
  52. }  

④C++ 普通实现方法

  1. #include <stdio.h>  
  2.   
  3. int* XRemove(int *Src, int Size,int *Dst, int &nSize)  
  4. {  
  5.     bool* Index = new bool[Size];  
  6.   
  7.     for(int i = 0; i < Size; ++i)  
  8.         Index[i] = true;  
  9.   
  10.     int* Crt = Dst;      
  11.   
  12.     nSize = 0;  
  13.     for(int x = 0; x < Size; ++x)  
  14.     {  
  15.         if(0 == Index[x])  
  16.             continue;  
  17.   
  18.         for(int y = x + 1; y < Size; ++y)  
  19.             if(Src[x] == Src[y])  
  20.                 Index[y] = false;  
  21.   
  22.         if(Index[x]){  
  23.             nSize ++;  
  24.             *Crt++ = Src[x];  
  25.         }  
  26.     }  
  27.   
  28.     delete [] Index;  
  29.     return Dst;  
  30. }  
  31.   
  32. void main()  
  33. {  
  34.     int Src[]={1,3,4,3,2,5,2};  
  35.   
  36.     int Size = sizeof(Src) / sizeof(int);  
  37.     int *Dst =  new int [Size];  
  38.   
  39.     int nSize;  
  40.     XRemove(Src, Size, Dst, nSize);  
  41.   
  42.     for(int i = 0;i < nSize;i ++)  
  43.         printf("%d ",Dst[i]);  
  44.   
  45.     printf("\n");  
  46. }  

二)去掉重复出现的数字

例如:int a[]={1,3,4,3,2,5,2};

输出:1 4 5

C++ 实现方法:

  1. #include <stdio.h>  
  2.   
  3. int* XRemove(int Dst[], int Src[], int Size,int &nSize)  
  4. {  
  5.     bool* Index = new bool[Size];  
  6.   
  7.     for(int i = 0; i < Size; ++i)  
  8.         Index[i] = true;  
  9.   
  10.     int* Crt = Dst;  
  11.     bool Sig;  
  12.   
  13.     nSize = 0;  
  14.     for(int x = 0; Sig = (x < Size); ++x)  
  15.     {  
  16.         if(Index[x] == 0)  
  17.             continue;  
  18.   
  19.         for(int y = x + 1; y < Size; ++y)  
  20.             if(Src[x] == Src[y])  
  21.                 Sig = Index[y] = false;  
  22.   
  23.         if(Sig){  
  24.             nSize ++;  
  25.             *Crt++ = Src[x];  
  26.         }  
  27.     }  
  28.   
  29.     delete [] Index;  
  30.     return Dst;  
  31. }  
  32.   
  33. void main()  
  34. {  
  35.     int Src[]={1,3,4,3,2,5,2};  
  36.   
  37.     int Size = sizeof(Src) / sizeof(int);  
  38.   
  39.     int *Dst =  new int [Size];  
  40.   
  41.     int nSize;  
  42.   
  43.     XRemove(Dst,Src, Size,nSize);  
  44.   
  45.     for(int i = 0;i < nSize;i ++)  
  46.         printf("%d ",Dst[i]);  
  47.   
  48.     printf("\n");  
  49. }  

小结:使用vector容器可以很快解决。看来还是要多掌握一些知识和技术,拓宽自己的研究。有些问题也许用另一种方法,其实很简单,执行效率也很高。那么,我们为何不尝试一下呢?从中想想,要注意的问题?

 

来源:http://blog.csdn.net/tianmohust/article/details/7027713

posted on 2013-10-13 17:14  猿人谷  阅读(699)  评论(0编辑  收藏  举报