C++ Primer(第五版)|練習題答案與解析(第八章:IO庫)
本博客主要記錄C++ Primer(第五版)中的練習題答案與解析。
參考:C++ Primer
C++ Primer
練習題8.1
編寫函數,接受一個istream &參數,返回值也是istream&。此函數必須從給定流中讀取數據,直至遇到文件結束標識符時停止。它將讀取的數據打印在標準輸出上。完成這些操作後,在返回流之前,對流進行復位,使其處於有效狀態。
練習題8.2
測試函數,調用cin。
#include <iostream>
#include <vector>
#include <string>
#include <initializer_list>
using namespace std;
istream& func(istream &is)
{
std::string buf;
while (is >> buf)
std::cout << buf << std::endl;
is.clear();
return is;
}
int main()
{
istream& is = func(std::cin);
std::cout << is.rdstate() << std::endl;
return 0;
}
測試:
abcd
abcd
efg
efg
^Z
0
練習題8.3
什麼情況下,下面的while循環會終止。
while (cin >> i) /* … */
- 遇到輸入結束符;
- 遇到輸入錯誤,比如i是int型,卻輸入了字符。
練習題8.4
編寫函數,以讀模式打開一個文件,將其內容讀入到一個string的vector中,將每一行作爲一個獨立的元素存於vector中。
#include <iostream>
#include <vector>
#include <string>
#include <initializer_list>
#include <fstream>
using namespace std;
void ReadFileToVec(const string& fileName, vector<string>& vec)
{
ifstream ifs(fileName);
if (ifs)
{
string buf;
while (std::getline(ifs, buf))
vec.push_back(buf);
}
}
int main()
{
vector<string> vec;
ReadFileToVec("book.txt", vec);
for (const auto &str : vec)
cout << str << endl;
return 0;
}
輸出:
hello world //是book.txt中的內容
練習題8.5
重寫上面的程序,將每個單詞作爲一個獨立的元素進行存儲。
void ReadFileToVec(const string& fileName, vector<string>& vec)
{
ifstream ifs(fileName);
if (ifs)
{
string buf;
while (ifs >> buf)
vec.push_back(buf);
}
}
int main()
{
vector<string> vec;
ReadFileToVec("book.txt", vec);
for (const auto &str : vec)
cout << str << endl;
return 0;
}
輸出:
hello
world
練習題8.6
重寫7.1.1節的書店程序,從一個文件中讀取交易記錄。將文件名作爲參數傳遞給main。
同P284頁一樣。
練習題8.7
修改上一個程序,將結果保存在一個文件中。將輸出文件名作爲第二個參數傳給main。
頭文件不變。
主程序:
#include <fstream>
#include <iostream>
#include "test.h"
using namespace std;
// nonmember functions
// member functions.
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
// nonmember functions
std::istream &read(std::istream &is, Sales_data &item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
}
std::ostream &print(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue;
return os;
}
Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum.combine(rhs);
return sum;
}
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) << endl;
total = trans;
}
}
print(output, total) << endl;
}
else
{
cerr << "No data?!" << endl;
}
return 0;
}
測試:
C:\Users\Administrator\Desktop\Test\bin\Debug>test.exe input.txt output.txt
input.txt:
0-201-78345-X 5 110
0-201-78346-X 9 839.2
output.txt:
0-201-78345-X 5 550
0-201-78346-X 9 7552.8
練習題8.8
修改上一題的程序,將結果追加到給定的文件末尾。對同一個輸出文件,運行程序至少兩次,檢驗數據是否得以保留。
把上一題ofstream ofs(argv[2]);
改爲ofstream ofs(argv[2], ofstream::app);
output.txt:
0-201-78345-X 5 550
0-201-78346-X 9 7552.8
0-201-78345-X 5 550
0-201-78346-X 9 7552.8
練習題8.9
使用8.1.2節第一個練習所編寫的函數打印一個istringstream對象的內容。
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
istream& func(istream &is)
{
std::string buf;
while (is >> buf)
std::cout << buf << std::endl;
is.clear();
return is;
}
int main()
{
std::istringstream iss("hello");
func(iss);
return 0;
}
練習題8.10
編寫程序,將來自一個文件中的行保存在一個vector中,然後使用一個istringstream從vector讀取數據元素,每次讀取一個單詞。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
ifstream ifs("book.txt");
if (!ifs)
{
cerr << "No data?" << endl;
return -1;
}
vector<string> vecLine;
string line;
while (getline(ifs, line))
vecLine.push_back(line);
for (auto &s : vecLine)
{
istringstream iss(s);
string word;
while (iss >> word)
cout << word << endl;
}
return 0;
}
測試
hello
world
練習題8.11
編寫程序,將來自一個文件中的行保存在一個vector中,然後使用一個istringstream從vector讀取數據元素,每次讀取一個單詞。
#include <iostream>
#include <fstream>
#include <sstream>
#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))
{
PersonInfo info;
record.clear();
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
for (auto &p : people)
{
std::cout << p.name << " ";
for (auto &s : p.phones)
std::cout << s << " ";
std::cout << std::endl;
}
return 0;
}
測試:
morgan 2015553216 8554846
^Z
morgan 2015553216 8554846
練習題8.12
我們爲什麼沒有在PersonInfo中使用類內初始化?
因爲這裏我們使用了聚合類,不需要類內初始化。
練習題8.13
重寫本節的電腦號碼程序,從一個命名文件而非cin讀取數據。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
struct PersonInfo {
string name;
vector<string> phones;
};
bool valid(const string& str)
{
return isdigit(str[0]);
}
string format(const string& str)
{
return str.substr(0,3) + "-" + str.substr(3,3) + "-" + str.substr(6);
}
int main()
{
ifstream ifs("phonenumbers.txt");
if (!ifs)
{
cerr << "no phone numbers?" << endl;
return -1;
}
string line, word;
vector<PersonInfo> people;
istringstream record;
while (getline(ifs, line))
{
PersonInfo info;
record.clear();
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
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())
cout << entry.name << " " << formatted.str() << endl;
else
cerr << "input error: " << entry.name
<< " invalid number(s) " << badNums.str() << endl;
}
return 0;
}
測試
lee 123-468-5465 231-322-1
bob 213-154-6546 201-325-454
練習題8.14
我們爲什麼要將entry和nums定義爲const auto&。
使用引用是因爲,都是類內string類型,引用可以避免拷貝,提高效率。
const是因爲函數內不改變對象的值。