删掉重复的数据

简介: 一)过滤重复的数据。即把原来重复的数据过滤,只保留一份。 例如:int a[]={1,3,4,3,2,5,2}; 输出:1 3 4 2 5 ①实现方法一(采用STL): [cpp] view plaincopy #include    #include    ...

 

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

例如: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

img_e00999465d1c2c1b02df587a3ec9c13d.jpg
微信公众号: 猿人谷
如果您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】
如果您希望与我交流互动,欢迎关注微信公众号
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

目录
相关文章
|
2月前
|
算法 前端开发
在系统中查找重复文件
在系统中查找重复文件
33 0
|
27天前
|
机器学习/深度学习 Python
获取重复的文件
使用 Python 3.10+ 的程序找出图片样本中的重复文件,依赖包 `NStudyPy`。通过计算文件的 MD5 值来识别重复项。核心函数 `get_repeat_file` 接受路径和递归选项,返回一个字典,键为 MD5,值为相同 MD5 的文件列表。`get_file_list` 和 `get_md5` 函数留待后续解释。安装 `NStudyPy`:`pip install -U NStudyPy`。
20 2
|
27天前
|
机器学习/深度学习 Python
删除指定文件夹重复的文件
这是一个Python脚本,用于删除指定文件夹(包括子目录)中的重复图片文件,基于文件的MD5值。程序依赖`NStudyPy`库,可通过`pip install -U NStudyPy`安装。核心函数`delete_repeat_file`接收路径和是否递归参数,调用未展示的`get_repeat_file`函数获取重复文件并删除。
19 1
|
2月前
|
存储 弹性计算 运维
从文件中删除重复的行
【4月更文挑战第29天】
26 1
|
2月前
【全网最简短代码】筛选出新数组中和旧数组的重复项,并和旧数组合并(往数组追加新的数据对象且去重,合并两个数组不重复数据)
【全网最简短代码】筛选出新数组中和旧数组的重复项,并和旧数组合并(往数组追加新的数据对象且去重,合并两个数组不重复数据)
|
设计模式 消息中间件 JavaScript
干掉 “重复代码”,这三种方式绝了!
干掉 “重复代码”,这三种方式绝了!
36927 2
干掉 “重复代码”,这三种方式绝了!
|
数据采集 JSON 数据格式
一日一技:如何处理配置文件中的重复值?
一日一技:如何处理配置文件中的重复值?
105 0
|
设计模式 算法 Java
干掉 “重复代码” 的技巧有哪些
软件工程师和码农最大的区别就是平时写代码时习惯问题,码农很喜欢写重复代码而软件工程师会利用各种技巧去干掉重复的冗余代码。
112 0
干掉 “重复代码” 的技巧有哪些
怎样去掉list里重复的数据(多种方法)
怎样去掉list里重复的数据(多种方法)
151 0
怎样去掉list里重复的数据(多种方法)
|
缓存
缓存键重复、冲突带来的数据读取错误
    第二次了,栽在这个问题上, 依然是出自缓存。     需要注意两个问题:     1.根据分类读的数据列表,缓存键需要带上分类ID/分类名称。     2.由于子模块比较多,缓存键命名的时候,整体搜一下是否已存在当前缓存键,否则容易出现多个模块公用一个缓存的现象,导致必有一处数据不对。
922 0