forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex8_10.cpp
41 lines (35 loc) · 905 Bytes
/
ex8_10.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
//
// ex8_10.cpp
// Exercise 8.10
//
// Created by pezy on 11/29/14.
//
// @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.
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using std::vector; using std::string; using std::ifstream; using std::istringstream; using std::cout; using std::endl; using std::cerr;
int main()
{
ifstream ifs("../data/book.txt");
if (!ifs)
{
cerr << "No data?" << endl;
return -1;
}
vector<string> vecLine;
string line;
while (getline(ifs, line))
vecLine.push_back(line);
for (auto &s : vecLine)
{
istringstream iss(s);
string word;
while (iss >> word)
cout << word << endl;
}
return 0;
}