vector 类型 怎么写入文件?

Alan S1 2006-01-29 08:30:03
vector<string> 类型 怎么写入文件?
...全文
269 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fireinsnow 2006-01-31
  • 打赏
  • 举报
回复
我想楼主的意思是打开文件用ios_base::binary格式吧,因为string没法用二进制写入文件中。
LYH_Studio 2006-01-29
  • 打赏
  • 举报
回复
上一贴漏了打
#include <string>
using namespace std;
LYH_Studio 2006-01-29
  • 打赏
  • 举报
回复
#include <vector>
#include <fstream>
#include <algorithm>

int main(int, char *)
{
vector<string> vec;
vec.push_back("liyehua");
vec.push_back("heting");
vec.push_back("chenzhao");
vec.push_back("linxin");

ofstream file("li.txt");

copy(vec.begin(), vec.end(), ostream_iterator<string>(file));

return EXIT_SUCCESS;
}
msjhonlili 2006-01-29
  • 打赏
  • 举报
回复
还是给出一个简要的例子吧,LZ可以参详

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main(int argc, char** argv)
{
/* 省略文件参数检查 */
ofstream outfile(argv[1]);
vector<string> vecOut;
... // 对vecOut进行操作
vector<string>::iterator it;
for(it = vecOut.begin(); it != vecOut.end(); ++it)
{
outfile << *it << endl;
}

return 0;
}

64,282

社区成员

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

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