C++ primer English fourth edition exercise page 301

 

C++ primer fourth edition page301

Write a program to store each line from a file in a vector<string>. Now use an istringstream to read each line

from the vector a word at a time.

#include<iostream>
#include
<string>
#include
<vector>
#include
<sstream>
using namespace std;

int main ()
{
  vector
<string> str;
  
string s;
  
while(getline(cin,s))
  
{
    str.push_back(s);
  }

  
  
string st;
  
for(vector<string>::iterator iter=str.begin();iter!=str.end();++iter)
  
{
    istringstream istr(
*iter);
    
while(istr>>st)
    
{
        cout
<<st<<endl;
    }

    
  }

  
return 0;

}
 
發佈了27 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章