-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path8.11.cpp
53 lines (48 loc) · 1.36 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
45
46
47
48
49
50
51
52
53
/*
* Exercise 8.11: 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
*
*
* By Faisal Saadatmand
*/
/*
* Answer: first, declare the unbound stream record outside the outer loop,
* than copy the string line into the stream at every iteration of the otter
* loop, and, finally, clear the stream just before looping back so it will be
* in a valid state on the next line copy.
*/
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
struct PersonInfo {
std::string name;
std::vector<std::string> phones;
};
std::ostream& print(std::ostream &os, const PersonInfo &person)
{
os << person.name;
for (const auto &number : person.phones)
std::cout << ' ' << number;
return os;
}
int main()
{
std::string line, word;
std::vector<PersonInfo> people;
std::istringstream record; // unbound istringstream
while (getline(std::cin, line)) {
PersonInfo info;
record.str(line); // copy line to stream
record >> info.name;
while (record >> word)
info.phones.push_back(word);
people.push_back(info);
record.clear(); // rest stream back to valid state
}
for (const auto &person : people) // print vector
print(std::cout, person) << '\n';
return 0;
}