Skip to content

Latest commit

 

History

History
 
 

ch08

Chapter 8. The IO Library

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.

istream& func(istream &is)
{
    std::string buf;
    while (is >> buf)
        std::cout << buf << std::endl;
    is.clear();
    return is;
}

Exercise 8.3:

What causes the following while to terminate?

while (cin >> i) /*  ...    */

putting cin in an error state cause to terminate. such as eofbit, failbit and badbit.

Exercise 8.12:

Why didn’t we use in-class initializers in PersonInfo?

Cause we need a aggregate class here. so it should have no in-class initializers.

Exercise 8.14:

Why did we declare entry and nums as const auto &?

  • cause they are all class type, not the built-in type. so reference more effective.
  • output shouldn't change their values. so we added the const.