如何读入一个文本并查找里面的内容?(用标准库)

hw110 2003-09-23 09:01:16
我用ifstream读入后,怎样查找它读入的文件内容呢,或者怎样把读入的文件流转换为vector<string>对象呢??才接触标准库不熟悉,谢谢。
...全文
100 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
aflyinghorse 2003-09-23
  • 打赏
  • 举报
回复
ifstream infile("c:\\test.txt", ios::in);
vector<string> words;//保存所有单词
string textline;

while ( getline(infile, textline, '\n') )//每次读一行
{
//把每行按空格分为单词
string::size_type pos = 0, prev_pos = 0;
while ( (pos = textline.find_first_of(' ', pos)) != string::npos)
{
words.push_back(textline.substr(prev_pos, pos-prev_pos));
pos++; prev_pos = pos;
}
words.push_back( textline.substr(prev_pos, pos-prev_pos) );
}
yym314 2003-09-23
  • 打赏
  • 举报
回复
#include <fstream>
#include <iterator>
#include <vector>
#include <iostream>

using namespace std;

int main()
{
ifstream infile("d:\\q.txt", ios::in );
vector<string> words;

copy( istream_iterator<string>( infile ),
istream_iterator<string>(),
back_inserter( words ) );

copy( words.begin(), words.end(),
ostream_iterator<string>( cout,"\r\n" ) );

char chEnd;
cin>>chEnd;
return 1;
}
yym314 2003-09-23
  • 打赏
  • 举报
回复
ifstream infile("c:\\test.txt", ios::in);
vector<string> words;//保存所有单词

copy( istream_iterator<string>( infile ),
istream_iterator<string>( ),
back_inserter(words) );
Andy84920 2003-09-23
  • 打赏
  • 举报
回复
像楼上的一样!

C++ PRIMER 第六章有详细的文本查询系统.

去下个电子书看看.

有源码的.

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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