本 Wiki 开启了 HTTPS。但由于同 IP 的 Blog 也开启了 HTTPS,因此本站必须要支持 SNI 的浏览器才能浏览。为了兼容一部分浏览器,本站保留了 HTTP 作为兼容。如果您的浏览器支持 SNI,请尽量通过 HTTPS 访问本站,谢谢!
Answers for chapter 8
Exercise 8.1: Write a function that takes and returns an istream&. The function should read the stream until it hits end-of-file. The function should print what it reads to the standard output. Reset the stream so that it is valid before returning the stream.
std::istream&
reader(std::istream& is) {
std::string word;
while(is >> word) {
std::cout << word << std::endl;
}
is.clear();
return is;
}
Exercise 8.2: Test your function by calling it, passing cin as an argument.
CODE: Q.1
Exercise 8.3: What causes the following while to terminate?
while (cin >> i) /* ... */
Execpt for goodbit, all other flags will cause while statement to terminate.
Exercise 8.4: Write a function to open a file for input and read its contents into a vector of strings, storing each line as a separate element in the vector.
CODE: Q.1
Exercise 8.5: Rewrite the previous program to store each word in a separate element.
CODE: Q.1
Exercise 8.6: Rewrite the bookstore program from § 7.1.1 (p. 256) to read its transactions from a file. Pass the name of the file as an argument to main (§ 6.2.5, p. 218).
Exercise 8.7: Revise the bookstore program from the previous section to write its output to a file. Pass the name of that file as a second argument to main.
CODE: Q.1
Exercise 8.8: Revise the program from the previous exercise to append its output to its given file. Run the program on the same output file at least twice to ensure that the data are preserved.
CODE: Q.1
Exercise 8.8: Revise the program from the previous exercise to append its output to its given file. Run the program on the same output file at least twice to ensure that the data are preserved.
CODE: Q.1
Exercise 8.8: Revise the program from the previous exercise to append its output to its given file. Run the program on the same output file at least twice to ensure that the data are preserved.
CODE: Q.1
The program in this section defined its istringstream object inside the outer while loop. What changes would you need to make if record were defined outside that loop? Rewrite the program, moving the definition of record outside the while, and see whether you thought of all the changes that are needed.
The program will print either nothing or whatever string “line” has by default.
The istringstream object defined in a loop is a temporary object. It is created, destroyed and bound to the newest line from the input each loop. That allows us to bind an isstringstream object to every line of the outer loop, then print every element in the line. If we choose to move the object definition out of the outer loop, it will be bound to the default, empty string “line” and will print out nothing(or default value in that string). The contents added to the “line” afterward through the getline function will not affect the result.
/* test result */
//input
aaa 111
bbb 222
ccc 333
ddd 444
//output, the string line has default value "Hello word."
Hello word.
CODE: Q.1
Exercise 8.12: Why didn’t we use in-class initializers in PersonInfo?
The Personalinfo class has two members, a string type member and a vector<string> type member. Both members can provide a safe default value. Hence, there is no need to specify initializers for them.
Exercise 8.13: Rewrite the phone number program from this section to read from a named file rather than from cin.
CODE: Q.1
Exercise 8.14: Why did we declare entry and nums as const auto &?