forked from zyl111ok/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex8_11.cpp
47 lines (42 loc) · 1.16 KB
/
ex8_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
45
46
47
//
// ex8_11.cpp
// Exercise 8.11
//
// Created by pezy on 11/29/14.
//
// @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;
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();
record.str(line);
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
}
for (auto &p : people)
{
std::cout << p.name << " ";
for (auto &s : p.phones)
std::cout << s << " ";
std::cout << std::endl;
}
return 0;
}