forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.11.cpp
44 lines (40 loc) · 1.09 KB
/
8.11.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
using std::vector;
using std::string;
using std::cin;
using std::istringstream;
using std::getline;
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(); // Note this line.
// The stream must be reset because after reading the first line, the
// stream's state is end-of-file, and calling `str(someString)` member
// function will not reset the stream's state. Thus, `clear()` must be
// called explicitly.
// However, the `open()` member funtion of `fstream` will automatically
// reset the stream's state if it succeeds.
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
for (const auto &e : people) {
std::cout << e.name << ": ";
for (const auto &p : e.phones)
std::cout << p << " ";
std::cout << std::endl;
}
return 0;
}