今天学习了一下vector容器,解决了这个问题,现附上代码:
#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\n",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!\n");
exit(1);
}
if(NULL == (fpOutput = fopen("D:\\output.txt","w")))
{
printf("File write error!\n");
exit(1);
}
Dedup(fpInput,fpOutput);
printf("\n");
fclose(fpInput);
fclose(fpOutput);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
FILE *fpInput,*fpOutput;
template <typename T>
void print_element(T value)
{
//print
//std::cout<<value<<std::endl;
//print into output.txt
fprintf(fpOutput,"%d ",value);
}
template <typename T>
unsigned int MakeUniqueArray(T* array,unsigned int length)
{
std::vector <T> v(array,array + length);
std::sort(v.begin(),v.end());
T* end = std::unique_copy(v.begin(),v.end(),array);
return end - array;
}
void main()
{
if(NULL == (fpInput = fopen("input.txt","r")))
{
printf("File read error!\n");
exit(1);
}
if(NULL == (fpOutput = fopen("output.txt","w")))
{
printf("File write error!\n");
exit(1);
}
//read
const int MAXLEN = 1024;
int array[MAXLEN];
int nCount = 0;
while(NULL == feof(fpInput))
{
fscanf(fpInput,"%d",&array[nCount]);
//printf("%d ",array[i]);
nCount ++ ;
}
void (*pfi)(int) = print_element;
unsigned int len = MakeUniqueArray(array,nCount);
std::for_each(array,array + len,print_element <int>);
//std::cin.get();
fclose(fpInput);
fclose(fpOutput);
}
纯C实现的代码:
#include <stdio.h>
#include <stdlib.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;
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()
{
FILE *fpInput,*fpOutput;
if(NULL == (fpInput = fopen("input.txt","r")))
{
printf("File read error!\n");
exit(1);
}
if(NULL == (fpOutput = fopen("output.txt","w")))
{
printf("File write error!\n");
exit(1);
}
//read
const int MAXLEN = 1024;
int array[MAXLEN];
int nCount = 0;
while(NULL == feof(fpInput))
{
fscanf(fpInput,"%d",&array[nCount]);
// printf("%d ",array[nCount]);
nCount ++ ;
}
int Dst[MAXLEN];
int nSize;
XRemove(Dst,array,nCount,nSize);
for(int i = 0; i < nSize;i ++)
//printf("%d ",Dst[i]);
fprintf(fpOutput,"%d ",Dst[i]);
fclose(fpInput);
fclose(fpOutput);
}