在txt文件中,查找并处理重复的单词

gdfjupiter 2007-12-03 08:38:01
有一个文本文件内容如下:
========分割线========

你好,很好
23 26003 测试你好
24 26666 视频网络的性能 等等
25 26003 测试你好
26 10023 这个测试句子
很好:是的
27 55210 没有了

========分割线========

现在要找出文件中的重复项如:

23 26003 测试你好
25 26003 测试你好

并在重复的后面加上: (重复)
即:
23 26003 测试你好
25 26003 测试你好(重复)

注意:这两句除了前面的序号不同后面的字都相同

最后的文件是这样子:
========分割线========

你好,很好
23 26003 测试你好
24 26666 视频网络的性能 等等
25 26003 测试你好(重复)
26 10023 这个测试句子
很好:是的
27 55210 没有了

========分割线========


写代码实现上面的功能,这个东西我想了很长时间没办法,求各位帮帮忙
...全文
474 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gdfjupiter 2007-12-04
  • 打赏
  • 举报
回复
多谢各位的回复,结贴
jixingzhong 2007-12-04
  • 打赏
  • 举报
回复
这个文本不复杂,可以自己解析。

读取 line;
分解获得序号以后的 string,
和 vector 中已有的string 比较(find 方法查找一下),如有重复,追加"(重复)",push_back()添加到vector中
continue ...

子串的获得,find方法等的使用,看:www.cppreference.com/cppstring/index.html
effective_person 2007-12-03
  • 打赏
  • 举报
回复

#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;
}


weiym 2007-12-03
  • 打赏
  • 举报
回复
#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;
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧