如何将txt文件读入vector变量中?

hhmmdd 2002-11-05 03:15:42
如题,txt文件中每一行对应vector<string>的一个元素
...全文
115 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
myy 2002-11-05
  • 打赏
  • 举报
回复
TopCat(令狐虫) 的方法更专业!献丑了...
myy 2002-11-05
  • 打赏
  • 举报
回复
1.new一个TStringList *ss,
ss->LoadFormFile("...");
再到入到vector中。
std::iterate vectot<string> ivs;
for(........

2.如果文件很大,可以用fopen,fgets,fclose....
TopCat 2002-11-05
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

void show(string str)
{
cout << str << endl;
}

int main()
{
vector<string> str_vect;
ifstream infile("c:\\test.txt");

if (!infile)
{
cerr << "文件打开失败" << endl;
return -1;
}

string str;
while (getline(infile, str, '\n'))
{
str_vect.push_back(str);
}

//显示结果
for_each(str_vect.begin(), str_vect.end(), show);

return 0;
}
wker 2002-11-05
  • 打赏
  • 举报
回复
关注。

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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