問題:編寫程序,將來自一個文件中的行保存在一個vector中, 然後視同一個istringstream從vector中讀取數據源素, 每次讀取一個單詞。
答案
1 #include<iostream>
2 #include<fstream>
3 #include<sstream>
4 #include<vector>
5 using namespace std;
6 int main() {
7 ifstream in("a.txt");
8 vector<string>strings;
9 vector<string>word;
10 string temp;
11 while (getline(in, temp))
12 strings.push_back(temp);
13 for(int i = 0; i != strings.size(); ++i) {
14 istringstream record(strings[i]);
15 while (record >> temp)
16 word.push_back(temp);
17 }
18 for (int i = 0; i != word.size(); ++i)
19 cout << word[i] << endl;
20 return 0;
21 }