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.
// // ex8_02.cpp // Exercise 8.2 // // Created by pezy on 11/27/14. // Copyright (c) 2014 pezy. All rights reserved. // // @Brief Test your function by calling it, passing cin as an argument
// // ex8_04.cpp // Exercise 8.4 // // Created by pezy on 11/9/14. // Copyright (c) 2014 pezy. All rights reserved. // // @Brief 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.
// // ex8_04.cpp // Exercise 8.4 // // Created by pezy on 11/9/14. // Copyright (c) 2014 pezy. All rights reserved. // // @Brief 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.
// // ex8_09.cpp // Exercise 8.9 // // Created by pezy on 11/29/14. // Copyright (c) 2014 pezy. All rights reserved. // // @Brief Use the function you wrote for the first exercise in § 8.1.2 (p.314) // to print the contents of an istringstream object. // @See Exercise 8.1
#include<iostream> #include<sstream> using std::istream;
// // ex8_10.cpp // Exercise 8.10 // // Created by pezy on 11/29/14. // Copyright (c) 2014 pezy. All rights reserved. // // @Brief Write a program to store each line from a file in a vector<string>. // Now use an istringstream to read each element from the vector a word // at a time.
// // ex8_11.cpp // Exercise 8.11 // // Created by pezy on 11/29/14. // Copyright (c) 2014 pezy. All rights reserved. // // @Brief 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.
#include<iostream> #include<sstream> #include<string> #include<vector> using std::vector; using std::string; using std::cin; using std::istringstream;
record.str(line);:将当前行 line 设置为
record 流的字符串内容,这样 record
流将以当前行的内容作为输入源。
record >> info.name;:从 record
流中读取一个字符串,存储到 info.name
中,这是当前行中的第一个字符串,即姓名。
while (record >> word) info.phones.push_back(word);:使用一个
while 循环从 record
流中连续读取字符串,直到流的末尾。每次读取的字符串都存储到
info.phones
向量中,这样就可以将当前行中的所有电话号码都存储到
info.phones 中。
people.push_back(info);:将存储了当前行信息的
info 对象添加到 people
向量中,这样就完成了当前行的信息存储。
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.
// // ex8_13.cpp // Exercise 8.13 // // Created by pezy on 11/29/14. // Copyright (c) 2014 pezy. All rights reserved. // // @Brief Rewrite the phone number program from this section to read from // a named file rather than from cin. // @See ex8_11.cpp
using std::vector; using std::string; using std::cin; using std::istringstream; using std::ostringstream; using std::ifstream; using std::cerr; using std::cout; using std::endl; using std::isdigit;