8.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
istream& iofunc(istream& is) {
string s;
while (is >> s) {
cout << s << endl;
}
is.clear();
return is;
}


int main() {

iofunc(cin);
system("pause");
return 0;
}
8.4
1
2
3
4
5
6
7
8
9
10
11
12
vector<string>v;
ifstream ifs;
ifs.open("ifile.txt", ios::in);
if (ifs.is_open()) //if(ifs>>s)逐个单词
{
string s;
while (getline(ifs,s)) {
v.push_back(s);
cout << s << endl;
}
ifs.close();
}
8.10
1
2
3
4
5
6
7
8
9
10
11
12
string line, word;
vector<string> vec;
while (getline(cin,line))
{
vec.push_back(line);
cout << vec.back() << endl;
istringstream record(vec.back());
while (record >> word) {
cout << word << endl;
}
}

8.11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
struct PersonInfo {
string name;
vector<string> phones;
};

int main() {
string line, word;
vector<PersonInfo> people;
istringstream record;

while (getline(cin, line)) {
record.str(line);
PersonInfo info;
record >> info.name;
while (record >> word) {
info.phones.push_back(word);
}
record.clear();//复位
people.push_back(info);
}

for (const auto &entry : people) {
cout << entry.name << " ";
for (const auto &ph : entry.phones) {
cout << ph << " ";
}
cout << endl;
}

return 0;
}