当前位置:首页 » 《关于电脑》 » 正文

【C++ Primer Plus习题】17.7

2 人参与  2024年10月03日 10:40  分类 : 《关于电脑》  评论

点击全文阅读


问题:

这里是引用
在这里插入图片描述

解答:

#include <iostream>#include <vector>#include <string>#include <fstream>#include <algorithm>using namespace std;const int LIMIT = 50;void ShowStr(const string& str);void GetStrs(ifstream& fin, vector<string>& v);class Store{private:string str;ofstream* fout;public:Store(ofstream&out):fout(&out){}bool operator()(const string& str);~Store(){}};void ShowStr(const string& str){cout << str << endl;}void GetStrs(ifstream& fin, vector<string>& v){unsigned int len;char* p;while (fin.read((char*)&len, sizeof len)){p = new char[len];fin.read(p,len);v.push_back(p);}}bool Store::operator()(const string& str){unsigned int len = str.length();if (fout->is_open()){fout->write((char*)&len, sizeof len);fout->write(str.data(), len);return true;}else{return false;}}int main(){vector<string> vostr;string temp;cout << "Enter strings (empty line to quit):\n";while (getline(cin, temp) && temp[0] != '\0')vostr.push_back(temp);cout << "Here is your input.\n";for_each(vostr.begin(), vostr.end(),ShowStr);ofstream fout("strings.txt", ios_base::in | ios_base::binary);for_each(vostr.begin(), vostr.end(), Store(fout));fout.close();vector<string>vistr;ifstream fin("strings.txt", ios_base::in | ios_base::binary);if (!fin.is_open()){cerr << "Could not open the file for input.\n";exit(EXIT_FAILURE);}GetStrs(fin, vistr);cout << "\nHere are the strings read from the file:\n";for_each(vistr.begin(), vistr.end(), ShowStr);return 0;}

拜了个拜!


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/167324.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 贵妃每天只想当咸鱼小说(萧兮兮洛清寒)(贵妃每天只想当咸鱼)整本+后续+结局在线阅读
  • 云纾君迟玉:+后续+番外半溪明月枕清风无删减小说在线无广告高口碑小说
  • 新章速递既负如来又负卿是什么小说(莫司淮唐乐薇)完本阅读无广告(莫司淮唐乐薇)
  • 林齐(林齐)火爆小说全集免费阅读_反转传奇林齐:结局+番外无弹窗最新章节笔趣阁(林齐)
  • 萧兮兮洛清寒小说(贵妃每天只想当咸鱼)起点章节+全篇阅读热门作品预订
  • 穿成塌房顶流,我靠爆料翻红(陈昭陈莹静)_穿成塌房顶流,我靠爆料翻红
  • (番外)+(全文)谢青霄林相宜(八零奉子成婚,死对头成了妻管严:全文+结局+番外)全文免费阅读无弹窗大结局_(谢青霄林相宜)最新章节列表_笔趣阁(八零奉子成婚,死对头成了妻管严:全文+结局+番外)
  • 傅修言沈知穗小说(沈知穗傅修言)小说***下载_章节前文+后续(傅修言沈知穗)
  • 陆译林初夏小说(替身攻略失败后摆烂了)全文免费阅读_(替身攻略失败后摆烂了)陆译林初夏小说最新章节列表
  • 全书浏览说好摸鱼打游戏,你爆杀华尔街?(王文斌方幻)_说好摸鱼打游戏,你爆杀华尔街?(王文斌方幻)全书结局
  • (重生后高傲妻子我不爱了)重生后高傲妻子我不爱了(梁言彻尹暖芸)无套路无弹窗全部章节列表
  • (番外)+(全文)傅修言沈知穗:番外+全文+后续(沈知穗傅修言)完整版小说阅读_傅修言沈知穗:番外+全文+后续免费阅读_笔趣阁(沈知穗傅修言)

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1