一)过滤重复的数据。即把原来重复的数据过滤,只保留一份。
例如:int a[]={1,3,4,3,2,5,2};
输出:1 3 4 2 5
①实现方法一(采用STL):
- #include <iostream>
- #include <algorithm>
- #include <set>
- using namespace std;
- void Dedup(FILE* infile,FILE* outfile)
- {
- int iTemp;
- unsigned int nCount = 0;
- typedef set<int> IntSet;
- IntSet iArray;
- while(NULL == feof(infile))
- {
- fscanf(infile,"%d",&iTemp);
- iArray.insert(iTemp);
- nCount ++;
- }
- printf("nCount = %d ",nCount);//total numbers
- for(IntSet::iterator iter = iArray.begin(); iter != iArray.end(); ++ iter)
- {
- fprintf(outfile,"%d ",*iter);
- }
- }
- int main()
- {
- FILE *fpInput,*fpOutput;
- if(NULL == (fpInput = fopen("D:\input.txt","r")))
- {
- printf("File read error! ");
- exit(1);
- }
- if(NULL == (fpOutput = fopen("D:\output.txt","w")))
- {
- printf("File write error! ");
- exit(1);
- }
- Dedup(fpInput,fpOutput);
- printf(" ");
- fclose(fpInput);
- fclose(fpOutput);
- return 0;
- }
②实现方法二(采用STL):
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- void Dedup(FILE* infile,FILE* outfile)
- {
- int iTemp;
- unsigned int nCount = 0;
- vector<int> iArray,iRes;
- while(NULL == feof(infile))
- {
- fscanf(infile,"%d",&iTemp);
- iArray.push_back(iTemp);
- nCount ++;
- }
- printf("nCount = %d ",nCount);//total numbers
- sort(iArray.begin(), iArray.end());//sort
- unique_copy(iArray.begin(), iArray.end(),back_inserter(iRes));//去掉重复的
- for(vector <int>::const_iterator iter = iRes.begin(); iter != iRes.end(); ++ iter)
- {
- fprintf(outfile,"%d ",*iter);
- }
- }
- int main()
- {
- FILE *fpInput,*fpOutput;
- if(NULL == (fpInput = fopen("D:\input.txt","r")))
- {
- printf("File read error! ");
- exit(1);
- }
- if(NULL == (fpOutput = fopen("D:\output.txt","w")))
- {
- printf("File write error! ");
- exit(1);
- }
- Dedup(fpInput,fpOutput);
- printf(" ");
- fclose(fpInput);
- fclose(fpOutput);
- return 0;
- }
③实现方法三(采用STL):
- #include <iostream>
- #include <string>
- #include <vector>
- #include <set>
- #include <algorithm>
- using namespace std;
- template <typename T>
- void print_element(T value)
- {
- cout<<value<<" ";
- }
- template <typename T>
- unsigned int MakeUniqueArray1(T* array, unsigned int length)
- {
- set<T> s(array, array + length);
- //copy(s.begin(), s.end(), array);
- printf("%d Number1. ",s.size());
- return s.size();
- }
- template <typename T>
- unsigned int MakeUniqueArray2(T* array, unsigned int length)
- {
- vector<T> v(array, array + length);
- sort(v.begin(), v.end());
- T* end = unique_copy(v.begin(), v.end(), array);
- printf("%d Number2. ",end - array);
- return end - array;
- }
- int main()
- {
- int a[] = {0,1,2,3,4,5,6,7,8,9,0,9,8,7,6,5,4,3,2,1};
- unsigned int len = sizeof(a) / sizeof(int);
- len = MakeUniqueArray1(a, len);
- len = MakeUniqueArray2(a, len);
- void (*pfi)(int) = print_element;
- for_each(a, a + len, print_element<int>);
- printf(" ");
- return 0;
- }
④C++ 普通实现方法
- #include <stdio.h>
- int* XRemove(int *Src, int Size,int *Dst, int &nSize)
- {
- bool* Index = new bool[Size];
- for(int i = 0; i < Size; ++i)
- Index[i] = true;
- int* Crt = Dst;
- nSize = 0;
- for(int x = 0; x < Size; ++x)
- {
- if(0 == Index[x])
- continue;
- for(int y = x + 1; y < Size; ++y)
- if(Src[x] == Src[y])
- Index[y] = false;
- if(Index[x]){
- nSize ++;
- *Crt++ = Src[x];
- }
- }
- delete [] Index;
- return Dst;
- }
- void main()
- {
- int Src[]={1,3,4,3,2,5,2};
- int Size = sizeof(Src) / sizeof(int);
- int *Dst = new int [Size];
- int nSize;
- XRemove(Src, Size, Dst, nSize);
- for(int i = 0;i < nSize;i ++)
- printf("%d ",Dst[i]);
- printf(" ");
- }
二)去掉重复出现的数字
例如:int a[]={1,3,4,3,2,5,2};
输出:1 4 5
C++ 实现方法:
- #include <stdio.h>
- int* XRemove(int Dst[], int Src[], int Size,int &nSize)
- {
- bool* Index = new bool[Size];
- for(int i = 0; i < Size; ++i)
- Index[i] = true;
- int* Crt = Dst;
- bool Sig;
- nSize = 0;
- for(int x = 0; Sig = (x < Size); ++x)
- {
- if(Index[x] == 0)
- continue;
- for(int y = x + 1; y < Size; ++y)
- if(Src[x] == Src[y])
- Sig = Index[y] = false;
- if(Sig){
- nSize ++;
- *Crt++ = Src[x];
- }
- }
- delete [] Index;
- return Dst;
- }
- void main()
- {
- int Src[]={1,3,4,3,2,5,2};
- int Size = sizeof(Src) / sizeof(int);
- int *Dst = new int [Size];
- int nSize;
- XRemove(Dst,Src, Size,nSize);
- for(int i = 0;i < nSize;i ++)
- printf("%d ",Dst[i]);
- printf(" ");
- }
小结:使用vector容器可以很快解决。看来还是要多掌握一些知识和技术,拓宽自己的研究。有些问题也许用另一种方法,其实很简单,执行效率也很高。那么,我们为何不尝试一下呢?从中想想,要注意的问题?