请教

Rick_ang 2005-08-31 11:29:31
请问如何从硬盘读入一个文本文档,并无重复地输出里面每个词出现的行号?
比如:文件是
abc asd asf abc
abc asd asf asssd asf
..
..

然后程序能输出:
abc: 1,2,....
asd: 1,2...
..
..

读入文件和记录词我是用fstream和vector完成的..但是请教怎么才能记录下每个词的行号啊?谢谢啦


...全文
169 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rick_ang 2005-08-31
  • 打赏
  • 举报
回复
但是怎样判断这个单词出现在第几行呢?我就是卡在这里了...
mituzhishi 2005-08-31
  • 打赏
  • 举报
回复
建议楼主看一下《C++ primer 4ed》的generic programming板块,

里面有一个很类似的例子,用stl实现,超级经典。

snowbirdfly 2005-08-31
  • 打赏
  • 举报
回复
是啊~
支持楼上~
你在读入文本文件时,每读入字符串时,同时也记录它的行号~
就如上面大哥所说的那样,用个结构~~
mituzhishi 2005-08-31
  • 打赏
  • 举报
回复
用数据结构 map <string,set<int> >

string 是词,set里存放行号。

比如 map <string,set<int> > Mymap;

Mymap["word"].insert(100);//将第100行的“word”单词存入。

并且,map和set自动杜绝重复,重复的插入忽略,省了楼主不少事。
zhouhuahai 2005-08-31
  • 打赏
  • 举报
回复
每一行用一个string记录,并附带加一行号不就行了?
struct{
string a;
int row;
}record;
Rick_ang 2005-08-31
  • 打赏
  • 举报
回复
谢谢..这个问题中我又学到了不少啊..呵呵
mituzhishi 2005-08-31
  • 打赏
  • 举报
回复
执行效果:

Please insert the word you want to query: the
the occurs 12 times.
(line 4) use of a key distinguishes them from the sequential
(line 8) keyvalue pairs. These types use the library pair class,
(line 9) defined in the utility header, to represent these pairs.
(line 11) that is a pair. The first member of the pair is a const
(line 12) key, and the second member is a value associated with
(line 16) with the same key.
(line 19) the sequential containers. However, the associative
(line 20) containers define some new operations and redefine the
(line 22) common with the sequential containers. The differences
(line 23) in the operations reflect the use of keys in associative
(line 28) elements in order by key. The begin operation yields the
(line 29) element with the lowest key. Incrementing that iterator
Please insert the word you want to query: order
order occurs 1 times.
(line 28) elements in order by key. The begin operation yields the
Please insert the word you want to query: ^Z
Press any key to continue

注意:ctrl + z 退出程序。
mituzhishi 2005-08-31
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>

using namespace std;

typedef vector<string>::size_type line_no;

int main()
{
string file("D:/C++_primer/file.txt");
ifstream infile(file.c_str());
if(!infile)
{
cout<<"Can not open file \""<<file<<"\""<<endl;
return EXIT_FAILURE;
}
string line,word;
map<string,set<line_no> >::iterator map_iter;
vector<string> lines;
map<string,set<line_no> > words;
while(getline(infile,line))
{
lines.push_back(line);
istringstream stringStream(line);
while(stringStream>>word)
{
words[word].insert(lines.size());
}
}

cout<<"Please insert the word you want to query: ";
while(cin>>word)
{
map_iter=words.find(word);
if(map_iter==words.end())
{
cout<<"Can not find \""<<word<<"\""<<endl;
cout<<"Please insert the word you want to query: ";
continue;
}

cout<<word<<" occurs "<<map_iter->second.size()<<" times.\n";
for(set<line_no>::const_iterator set_iter=map_iter->second.begin();set_iter!=map_iter->second.end();++set_iter)
{
cout<<"\t(line "<<*set_iter<<") "<<lines.at(*set_iter-1)<<endl;
}
cout<<"Please insert the word you want to query: ";
}

return 0;
}
lyclowlevel 2005-08-31
  • 打赏
  • 举报
回复
up。《C++ Primer》中确实有非常类似的,你看一下。
mituzhishi 2005-08-31
  • 打赏
  • 举报
回复
我告诉你吧,你一次读一行,记住行号,

然后分析每一行的单词,存放在map <string,set<int> >中,

其中必然用到getline()和istringstream.

建议楼主看一下《C++ primer 4ed》的generic programming板块,

里面有一个很类似的例子,用stl实现,超级经典。

里面的例子和你的问题一样。

guofu_x 2005-08-31
  • 打赏
  • 举报
回复

建议楼主看一下《C++ primer 4ed》的generic programming板块,

里面有一个很类似的例子,用stl实现,超级经典。

=====================================================
是啊,那里面讲的非常详细,很好的
Rick_ang 2005-08-31
  • 打赏
  • 举报
回复
UP一下...大家帮帮我...

64,651

社区成员

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

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