64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
vector<string> v;
fstream file;
string filename;
cout<<"Input file's name:";
cin>>filename;
string str;
file.open(filename.c_str());
if(!file.is_open())
{
cout<<"fail to open this file!"<<endl;
return 1;
}
while(file>>str)
{
v.push_back(str);
getline(file,str);
if(str==" ")
continue;
v.push_back(str);
}
vector<string>::iterator iter=v.begin();
while(iter!=v.end())
{
vector<string>::iterator it=iter;
for(++it;it!=v.end();it++)
{
if((*iter)==(*it))
{
(*it)=(*it)+"(重复)";
}
}
iter++;
}
int k=0;
for(iter=v.begin();iter!=v.end();iter++)
{
if(k%2==0)
cout<<endl;
k++;
cout<<*iter<<" ";
}
cout<<endl;
return 1;
}
#include "stdafx.h"
#include <fstream>
#include <string>
#include <set>
using namespace std;
bool checkFile(const char* pSrcFile, const char* pDestFile)
{
ifstream inf(pSrcFile);
ofstream outf(pDestFile);
set<string> setLine;
if(inf && outf)
{
string strLine;
while(getline(inf, strLine))
{
bool bDuplicate(false);
int nStart = strLine.find_first_of(' ', 0);//first space
if(nStart != string::npos)
{
int nEnd = strLine.find_first_not_of(' ', nStart);//first space end
if(nEnd != string::npos)
{
string strSub = strLine.substr(nEnd, strLine.size() - nEnd);
if(setLine.find(strSub) != setLine.end())
{
bDuplicate = true;
}
else
{
setLine.insert(strSub);
}
}
}
if(bDuplicate)
{
strLine += "(重复)";
}
strLine += "\n";
outf.write(strLine.c_str(), strLine.size());
}
outf.flush();
return true;
}
return false;
}
int main(int argc, char *argv[])
{
checkFile("c:\\test1.txt", "c:\\test2.txt");
system("pause");
return 0;
}