习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html

第8章 IO库


练习8.1

istream &iofunc(istream &is) {
string s;
while (is >> s) {
cout << s << endl;
}
is.clear();
return is;
}

练习8.2

#include<iostream>
#include<string>
using namespace std; istream &iofunc(istream &is) {
string s;
while (is >> s) {
cout << s << endl;
}
is.clear();
return is;
} int main() {
iofunc(cin);
return 0;
}

练习8.3

badbit、failbit和eofbit任一个被置位,则检测流状态的条件会失败。

练习8.4

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std; int fileToVector(string fileName,vector<string> &svec){
ifstream inFile(fileName);
if (!inFile) {
return 1;
}
string s;
while (getline(inFile, s)) {
svec.push_back(s);
}
inFile.close();
if (inFile.eof()) {
return 4;
}
if (inFile.bad()) {
return 2;
}
if (inFile.fail()) {
return 3;
}
}
int main() {
vector<string> svec;
string fileName, s;
cout << "Enter fileName:" << endl;
cin >> fileName;
switch (fileToVector(fileName, svec))
{
case 1:
cout << "error: can not open file: " << fileName << endl;
return -1;
case 2:
cout << "error: system failure." << endl;
return -1;
case 3:
cout << "error: read failure." << endl;
return -1;
}
cout << "向量里面的内容:" << endl;
for (vector<string>::iterator iter = svec.begin();iter != svec.end();++iter)
cout << *iter << endl;
return 0;
}

练习8.5

#include <iostream>
#include <fstream>
#include <string>
#include <vector> using namespace std; int fileToVector(string fileName, vector<string>& svec) {
ifstream inFile(fileName.c_str());
if (!inFile) {
return 1;
}
string s; //习题8.4一次输入一行
//while (getline(inFile, s)) {
// svec.push_back(s);
//} //习题8.5一次一个单词
while (inFile >> s) {
svec.push_back(s);
}
inFile.close();
if (inFile.eof()) {
return 4;
}
if (inFile.bad()) {
return 2;
}
if (inFile.fail()) {
return 3;
}
} int main() {
cout << "测试下" << endl;
vector<string> svec;
string fileName, s;
cout << "Enter filename: ";
cin >> fileName;
switch (fileToVector(fileName,svec))
{
case 1:
cout << "error: can not open file: " << fileName << endl;
return -1;
case 2:
cout << "error: system failure." << endl;
return -1;
case 3:
cout << "error: read failure." << endl;
return -1;
} cout << "向量里面的内容:" << endl;
for (vector<string>::iterator iter = svec.begin();iter != svec.end();++iter)
cout << *iter << endl;
return 0;
}

练习8.6-8.7

#include <iostream>
#include <string>
#include <fstream> using namespace std; class Sales_data {
public:
Sales_data() {}
Sales_data(std::string bN, unsigned sold, double reven) :bookNo(bN), units_sold(sold), revenue(reven) {}
std::string isbn() const { return this->bookNo; }
Sales_data& combine(const Sales_data &rhs) {
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
double avg_price() const {
if (units_sold) {
return revenue / units_sold;
}
else return 0;
}
Sales_data add(const Sales_data &lhs, const Sales_data &rhs) {
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
public:
std::string bookNo; //书号
unsigned units_sold;
double revenue;
}; istream &read(istream &is, Sales_data &item) {
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = item.units_sold * price;
return is;
} ostream &print(ostream &os, const Sales_data &item) {
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price()<<"\n";
return os;
} int main(int argc, char **argv)
{
ifstream input(argv[1]);
ofstream output(argv[2]); Sales_data total; if (read(input, total))
{
Sales_data trans; while (read(input, trans))
{
if (total.isbn() == trans.isbn())
{
total.combine(trans);
}
else
{
print(output, total);
cout << endl;
total = trans;
}
}
print(output, total);
cout << endl;
return 0;
}
else
{
cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
}

练习8.8

ofstream output(argv[2],ofstream::app);

练习8.9

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
using namespace std; istream &iofunc(istream &is) {
string s;
while (is >> s) {
cout << s << endl;
}
is.clear();
return is;
} int main() {
string sss;
cin >> sss;
istringstream iss(sss);
iofunc(iss);
system("pause");
return 0;
}

练习8.10

#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<sstream> using namespace std; int main() {
string infile = "test.txt";
vector<string> svec;
ifstream in(infile); if (in) {
string buf;
while (getline(in, buf)) {
svec.push_back(buf);
}
}
else {
cerr << "can not open the file:" << infile << endl;
}
for (auto s : svec) {
istringstream iss(s);
string word;
while (iss >> word) {
cout << word << endl;
}
} system("pause");
return 0;
}

练习8.11

#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<string> using namespace std; struct PersonInfo {
string name;
vector<string> phones;
}; int main() {
string line, word;
vector<PersonInfo> people;
istringstream record; while (getline(cin, line)) {
record.str(line);
PersonInfo info;
record >> info.name;
while (record >> word) {
info.phones.push_back(word);
}
record.clear();
people.push_back(info);
} for (const auto &entry : people) {
cout << entry.name << " ";
for (const auto &ph : entry.phones) {
cout << ph << " ";
}
cout << endl;
} return 0;
}

练习8.12

vector和string在定义后自动初始化。

练习8.13

#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<string> using namespace std; struct PersonInfo {
string name;
vector<string> phones;
}; int main() {
cout << "Please input the fileName:" << endl;
string infile;
cin >> infile;
ifstream in(infile); if (!in) {
cerr << "can not open the file: " << infile << endl;
return 0;
} string line, word;
vector<PersonInfo> people;
istringstream record; while (getline(in, line)) {
record.str(line);
PersonInfo info;
record >> info.name;
while (record >> word) {
info.phones.push_back(word);
}
record.clear();
people.push_back(info);
} for (const auto &entry : people) {
cout << entry.name << " ";
for (const auto &ph : entry.phones) {
cout << ph << " ";
}
cout << endl;
}
system("pause");
return 0;
}

练习8.14

无需修改所以用const,另外引用传值更快。

C++Primer第五版——习题答案详解(七)的更多相关文章

  1. C++Primer第五版——习题答案详解(一)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...

  2. C++Primer第五版——习题答案详解(二)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...

  3. C++Primer第五版——习题答案详解(三)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...

  4. C++Primer第五版——习题答案详解(四)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第5章 语句 练习5.9 #include<iostream> #inclu ...

  5. C++Primer第五版——习题答案详解(五)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...

  6. C++Primer第五版——习题答案详解(六)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public: std:: ...

  7. C++Primer第五版——习题答案详解(八)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第9章 顺序容器 练习9.1 a.list,需要按字典序插入,可能插入位置在中间 b.d ...

  8. C++Primer第五版——习题答案详解(九)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第10章 泛型算法 练习10.1 #include<iostream> #i ...

  9. C++Primer第五版——习题答案详解(十)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第11章 关联容器 练习11.3 #include<iostream> #i ...

随机推荐

  1. 【OS】Heap & Stack

    操作系统概念的堆.栈不同于数据结构的堆.栈. C 语言中,一切指针占 4 字节,这意味着指针指向 RAM 中的地址可以有 232 个,最小的地址是 0,最大的地址是 231 - 1. (一)堆: 堆空 ...

  2. QSplineSeries QChartView绘制曲线

    参考资料: https://www.qtdebug.com/qtbook-paint-smooth-curve-qchart/ https://blog.csdn.net/liang19890820/ ...

  3. Swiper.js使用及API介绍

    Swiper.js详细使用教程http://www.swiper.com.cn/api/start/2014/1218/140.html

  4. asp.net数据加载进度和模态窗口的完美打开,而且窗口不被阻止

    采用jquery的技术打开模态窗口,效果肯定不错,但是微软的asp.net ajax就无法用了,例如updatepanel面板和updateprogress就看不到效果,也就是jquery与asp.n ...

  5. unity中将多张图片进行椭圆运动

    GameObject canvas; ; ; ; ; Dictionary<int,GO> storeItem; GameObject main; public static bool r ...

  6. Linux 搭建Hadoop集群 成功

    内容基于(自己的真是操作步骤编写) Linux 搭建Hadoop集群---Jdk配置 Linux 搭建Hadoop集群 ---SSH免密登陆 一:下载安装 Hadoop 1.1:下载指定的Hadoop ...

  7. Linux中修改环境变量及生效方法(永久、临时)环境变量查看

    参考link:https://blog.csdn.net/u011630575/article/details/49839893 在项目中有一次帮忙组里搭环境时遇见了这部分的相关操作,记录一下.

  8. ipone 5s上,字体rem遇到的问题

    webapp中,12px的字体,利用rem实现自适应布局, 发现只有在ipone 5s中字体超大, 这两个class元素中字体一样大小,发现上面元素字体在ipone 5s中很大, 后来验证问题在哪里, ...

  9. 外网如何访问web项目holer实现篇

    外网访问WEB 内网主机上安装了WEB服务器,只能在局域网内访问,怎样从公网也能访问本地WEB应用? 本文将介绍使用holer实现的具体步骤. 1. 准备工作 1.1 安装Java 1.7及以上版本 ...

  10. Python全栈之路----函数----参数

    参数可以让你的函数更灵活,不只能做死的动作,还可以根据调用时传参的不同决定函数内部的执行流程. 形参:只有在被调用时才分配内存单元,在调用结束时,即可释放所分配的内存单元.因此形参只在函数内部有效.函 ...