专栏C++学习笔记
《C++ Primer》学习笔记/习题答案 总目录
——————————————————————————————————————————————————————
- 《C++ Primer》学习笔记(八):标准 IO 库
📚💻 Cpp-Prime5 + Cpp-Primer-Plus6 源代码和课后题
第8章 - 标准 IO 库
练习8.1
编写函数,接受一个 istream&
参数,返回值类型也是 istream&
。此函数须从给定流中读取数据,直至遇到文件结束标识时停止。它将读取的数据打印在标准输出上。完成这些操作后,在返回流之前,对流进行复位,使其处于有效状态。
解:
#include<iostream>
#include<stdexcept>
using namespace std;
istream & f(istream & in){
int v;
while (in >> v, !in.eof()) // 直到遇到文件结束符才停止读取
{
if (in.bad())
throw runtime_error("IO流错误");
if (in.fail()){
cerr << "数据错误,请重试:" << endl;
in.clear();
in.ignore(100, '\n');
continue;
}
cout << v << endl;
}
in.clear();
return in;
}
int main()
{
cout << "请输入一些整数,按Ctrl+Z结束" << endl;
f(cin);
system("pause");
return 0;
}
练习8.2
测试函数,调用参数为 cin
。
解:
练习8.3
什么情况下,下面的 while
循环会终止?
while (cin >> i) /* ... */
解:
遇到了文件结束符,或者遇到了IO流错误,或者读入了无效数据。
练习8.4
编写函数,以读模式打开一个文件,将其内容读入到一个 string
的 vector
中,将每一行作为一个独立的元素存于 vector
中。
解:
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
ifstream in("book_sales");
if (!in){
cerr << "无法打开输入文件" << endl;
system("pause");
return -1;
}
string line;
vector<string> words;
while (getline(in,line))
{
words.push_back(line);
}
in.close();
vector<string>::const_iterator it = words.begin();
while (it!=words.end())
{
cout << *it << endl;
++it;
}
system("pause");
return 0;
}
练习8.5
重写上面的程序,将每个单词作为一个独立的元素进行存储。
解:
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
ifstream in("book_sales");
if (!in){
cerr << "无法打开输入文件" << endl;
system("pause");
return -1;
}
string line;
vector<string> words;
while (in >> line) // 改动
{
words.push_back(line);
}
in.close();
vector<string>::const_iterator it = words.begin();
while (it!=words.end())
{
cout << *it << endl;
++it;
}
system("pause");
return 0;
}
练习8.6
重写7.1.1节的书店程序,从一个文件中读取交易记录。将文件名作为一个参数传递给 main
。
解:
#include<iostream>
#include<fstream>
#include"Sales_data.h"
using namespace std;
int main(){
ifstream in("book_sales8");
if (!in){
cerr << "无法打开输入文件" << endl;
system("pause");
return -1;
}
Sales_data total; //保存下一条交易记录的变量
//读入第一条交易记录,并确保有数据可以处理
if (in >> total){
Sales_data trans; //保存和的变量
//读入并处理剩余交易记录
while (in >> trans){
//如果我们仍在处理相同的书
if (total.isbn() == trans.isbn())
total += trans; //更新总销售额
else{
//打印前一本书的结果
cout << total << endl;
total = trans; //total现在表示下一本书的销售额
}
}
cout << total << endl; //打印最后一本书的结果
}
else{
//没有输入!警告读者
cerr << "No data?!" << endl;
return -1; // 表示失败
}
system("pause");
return 0;
}
练习8.7
修改上一节的书店程序,将结果保存到一个文件中。将输出文件名作为第二个参数传递给 main
函数。
解:
#include<iostream>
#include<fstream>
#include"Sales_data.h"
using namespace std;
int main(){
/*if (argc != 3){
cerr << "请给出输入、输出文件名" << endl;
return -1;
}*/
ifstream in("book_sales8");
if (!in){
cerr << "无法打开输入文件" << endl;
system("pause");
return -1;
}
ofstream out("makefile.txt");
if (!out){
cerr << "无法打开输出文件" << endl;
system("pause");
return -1;
}
Sales_data total; //保存下一条交易记录的变量
//读入第一条交易记录,并确保有数据可以处理
if (in >> total){
Sales_data trans; //保存和的变量
//读入并处理剩余交易记录
while (in >> trans){
//如果我们仍在处理相同的书
if (total.isbn() == trans.isbn())
total += trans; //更新总销售额
else{
//打印前一本书的结果
out << total << endl;
cout << total << endl;
total = trans; //total现在表示下一本书的销售额
}
}
out << total << endl;
cout << total << endl; //打印最后一本书的结果
}
else{
//没有输入!警告读者
cerr << "No data?!" << endl;
return -1; // 表示失败
}
system("pause");
return 0;
}
练习8.8
修改上一题的程序,将结果追加到给定的文件末尾。对同一个输出文件,运行程序至少两次,检验数据是否得以保留。
解:
上题的 ofstream out(argv[2]);
改为 ofstream out(argv[2], ofstream::app);
练习8.9
使用你为8.1.2节第一个练习所编写的函数打印一个 istringstream
对象的内容。
解:
#include<iostream>
#include<sstream>
#include<string>
#include<stdexcept>
using namespace std;
istream & f(istream & in){
string v;
while (in >> v, !in.eof()) // 直到遇到文件结束符才停止读取
{
if (in.bad())
throw runtime_error("IO流错误");
if (in.fail()){
cerr << "数据错误,请重试:" << endl;
in.clear();
in.ignore(100, '\n');
continue;
}
cout << v << endl;
}
in.clear();
return in;
}
int main()
{
ostringstream msg;
msg << "C++ Primer 第五版" << endl;
istringstream in(msg.str());
f(in);
system("pause");
return 0;
}
练习8.10
编写程序,将来自一个文件中的行保存在一个 vector
中。然后使用一个 istringstream
从 vector
读取数据元素,每次读取一个单词。
解:
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
int main()
{
ifstream in("book_sales8");
if (!in){
cerr << "无法打开输入文件" << endl;
system("pause");
return -1;
}
string line;
vector<string> words;
while (getline(in, line))
{
words.push_back(line);
}
in.close();
vector<string>::const_iterator it = words.begin();
while (it != words.end())
{
istringstream line_str(*it);
string word;
while (line_str >> word)
{
cout << word << " ";
}
cout << endl;
++it;
}
system("pause");
return 0;
}
练习8.11
本节的程序在外层 while
循环中定义了 istringstream
对象。如果 record
对象定义在循环之外,你需要对程序进行怎样的修改?重写程序,将 record
的定义移到 while
循环之外,验证你设想的修改方法是否正确。
解:
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
struct PersonInfo{
string name;
vector<string> phones;
};
int main(){
string line, word;
vector<PersonInfo> people;
istringstream record;
while (getline(cin, line)){
PersonInfo info;
record.clear();
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
system("pause");
return 0;
}
练习8.12
我们为什么没有在 PersonInfo
中使用类内初始化?
解:
由于每个人的电话号码数量不固定,因此更好的方式不是通过类内初始化指定人名和所有电话号码,而是在缺省初始化之后,在程序中设置人名并逐个添加电话号码。
练习8.13
重写本节的电话号码程序,从一个命名文件而非 cin
读取数据。
解:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct PersonInfo {
string name;
vector<string> phones;
};
bool valid(const string &s)
{
return true;
}
string format(const string &s)
{
return s;
}
int main()
{
string line, word;
vector<PersonInfo> people;
istringstream record;
//if (argc != 2){
// cerr << "请给出输入、输出文件名" << endl;
// return -1;
//}
ifstream in("book_sales");
if (!in){
cerr << "无法打开输入文件" << endl;
system("pause");
return -1;
}
while (getline(in, line))
{
PersonInfo info;
record.clear();
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
ostringstream os;
for (const auto &entry : people)
{
ostringstream formatted, badNums;
for (const auto &nums : entry.phones){
if (!valid(nums))
badNums << " " << nums;
else
formatted << " " << format(nums);
}
if (badNums.str().empty())
os << entry.name << " " << formatted.str() << endl;
else
cerr << "input error: " << entry.name
<< " invalid number(s) " << badNums.str() << endl;
}
cout << os.str() << endl;
system("pause");
return 0;
}
练习8.14
我们为什么将 entry
和 nums
定义为 const auto&
?
解:
这两条语句分别使用范围 for
语句枚举 people
中所有项(人)和每项的 phones
中的所有项(电话号码)。
-
使用
const
表明在循环中不会改变这些项的值; -
auto
是请求编译器依据vector
元素类型来推断出entry
和nums
的类型,既简化代码又避免出错; -
使用引用的原因是,
people
和phones
的元素分别是结构对象和字符串对象,使用引用即可避免对象拷贝。
如果想要更多的资源,欢迎关注 @我是管小亮,文字强迫症MAX~
回复【福利】即可获取我为你准备的大礼,包括C++,编程四大件,NLP,深度学习等等的资料。
想看更多文(段)章(子),欢迎关注微信公众号「程序员管小亮」~