C/C++ 學習筆記:istringstream、ostringstream、stringstream 類介紹 和 stringstream類 clear函數的真正用途

0、C++的輸入輸出分爲三種:

(1)基於控制檯的I/O

    

(2)基於文件的I/O

        

(3)基於字符串的I/O

          

1、頭文件

[cpp] view plaincopyprint?

  1. #include <sstream>  

2、作用

istringstream類用於執行C++風格的字符串流的輸入操作。 

ostringstream類用於執行C++風格的字符串流的輸出操作。 

strstream類同時可以支持C++風格的串流的輸入輸出操作。

3、具體分析

istringstream類

描述:從流中提取數據,支持 >> 操作

這裏字符串可以包括多個單詞,單詞之間使用空格分開

  1. istringstream的構造函數原形:  
  2. istringstream::istringstream(string str);  

初始化:使用字符串進行初始化

  1. istringstream istr("1 56.7");  
  2. istr.str("1 56.7");//把字符串"1 56.7"存入字符串流中   

使用:我們可以使用分解點獲取不同的數據,完成 字符串 到 其他類型 的轉換

常用成員函數:

 

  1. str():使istringstream對象返回一個string字符串  

舉例:把字符串類型的數據轉換爲其他類型

 

  1. #include <iostream>   
  2. #include <sstream>   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     istringstream istr("1 56.7");  
  7.   
  8.     cout<<istr.str()<<endl;//直接輸出字符串的數據 "1 56.7"   
  9.       
  10.     string str = istr.str();//函數str()返回一個字符串   
  11.     cout<<str<<endl;  
  12.       
  13.     int n;  
  14.     double d;  
  15.   
  16.     //以空格爲界,把istringstream中數據取出,應進行類型轉換   
  17.     istr>>n;//第一個數爲整型數據,輸出1   
  18.     istr>>d;//第二個數位浮點數,輸出56.7   
  19.   
  20.     //假設換下存儲類型   
  21.     istr>>d;//istringstream第一個數要自動變成浮點型,輸出仍爲1   
  22.     istr>>n;//istringstream第二個數要自動變成整型,有數字的階段,輸出爲56   
  23.   
  24.     //測試輸出   
  25.     cout<<d<<endl;  
  26.     cout<<n<<endl;  
  27.     system("pause");  
  28.     return 1;  
  29. }  

舉例2:把一行字符串放入流中,單詞以空格隔開。之後把一個個單詞從流中依次讀取到字符串

  1. #include <iostream>   
  2. #include <sstream>   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     istringstream istr;  
  7.     string line,str;  
  8.     while (getline(cin,line))//從終端接收一行字符串,並放入字符串line中   
  9.     {  
  10.         istr.str(line);//把line中的字符串存入字符串流中   
  11.         while(istr >> str)//每次讀取一個單詞(以空格爲界),存入str中   
  12.         {  
  13.             cout<<str<<endl;  
  14.         }  
  15.     }  
  16.     system("pause");  
  17.     return 1;  
  18. }  

輸入:123 34 45

輸出:

123  換行 34 換行 45

ostringstream類

描述:把其他類型的數據寫入流(往流中寫入數據),支持<<操作

  1. ostringstream的構造函數原形:  
  2. ostringstream::ostringstream(string str);  

初始化:使用字符串進行初始化

  1. ostringstream ostr("1234");  
  2. ostr.str("1234");//把字符串"1234"存入字符串流中  

舉例:

  1. #include <iostream>   
  2. #include <sstream>   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     //初始化輸出字符串流ostr   
  7.     ostringstream ostr("1234");  
  8.     cout<<ostr.str()<<endl;//輸出1234   
  9.       
  10.     ostr.put('5');//字符4頂替了1的位置   
  11.     cout<<ostr.str()<<endl;//輸出5234   
  12.   
  13.     ostr<<"67";//字符串67替代了23的位置,輸出5674   
  14.     string str = ostr.str();  
  15.     cout<<str<<endl;  
  16.     system("pause");  
  17.     return 1;  
  18. }  

stringstream類

描述:是對istringstream和ostringstream類的綜合,支持<<, >>操作符,可以進行字符串到其它類型的快速轉換

  1. stringstream的構造函數原形如下:  
  2. stringstream::stringstream(string str);  

初始化:使用字符串進行初始化

  1. stringstream str("1234");  
  2. str.str("1234");//把字符串"1234"存入字符串流中  

作用:

1、stringstream通常是用來做數據轉換的

2、將文件的所有數據一次性讀入內存

舉例1:基本數據類型變字符串

  1. /*基本數據類型變字符串*/  
  2. #include <fstream>   
  3. #include <iostream>   
  4. #include <sstream>   
  5. using namespace std;  
  6. int main()  
  7. {  
  8.     /*整型變字符串*/  
  9.     int n = 10;  
  10.     string str;  
  11.     stringstream stream;  
  12.       
  13.     stream << n;  
  14.     stream >> str;  
  15.       
  16.     cout<<str<<endl;  
  17.     stream.clear();//多次使用stringstream,要先清空下,不能使用stream.str("");否則下面輸出10   
  18.   
  19.     /*char* 變 string*/  
  20.     char cStr[10] = "china";  
  21.       
  22.     stream << cStr;  
  23.     stream >> str;  
  24.   
  25.     cout<<str<<endl;  
  26.     system("pause");  
  27.     return 1;  
  28. }  
}

舉例2:字符串變基本數據類型

  1. /*字符串變基本數據類型*/  
  2. #include <fstream>   
  3. #include <iostream>   
  4. #include <sstream>   
  5. using namespace std;  
  6. int main()  
  7. {  
  8.     /*字符串 變 double*/  
  9.     double n;  
  10.     string str = "12.5";  
  11.     stringstream stream;  
  12.       
  13.     stream << str;  
  14.     stream >> n;  
  15.       
  16.     cout<<n<<endl;  
  17.     stream.clear();//多次使用stringstream,要先清空下,不能使用stream.str("");   
  18.   
  19.     /*string 變 char* */  
  20.     string str1 = "china";  
  21.     char cStr[10];  
  22.       
  23.     stream << str1;  
  24.     stream >> cStr;  
  25.   
  26.     cout<<cStr<<endl;//輸出china   
  27.     system("pause");  
  28.     return 1;  
  29. }  
 注意:
  1. #include <iostream>   
  2. #include <sstream>   
  3. using namespace std;  
  4.   
  5. int main(int argc,char *argv[])  
  6. {  
  7.     std::stringstream stream;  
  8.     string str;  
  9.     while(1)  
  10.     {     
  11.         //clear(),這個名字讓很多人想當然地認爲它會清除流的內容。   
  12.         //實際上,它並不清空任何內容,它只是重置了流的狀態標誌而已!   
  13.         stream.clear();    
  14.   
  15.         // 去掉下面這行註釋,清空stringstream的緩衝,每次循環內存消耗將不再增加!   
  16.         //stream.str("");         
  17.   
  18.         stream<<"sdfsdfdsfsadfsdafsdfsdgsdgsdgsadgdsgsdagasdgsdagsadgsdgsgdsagsadgs";  
  19.         stream>>str;     
  20.   
  21.         //測試輸出每次循環,你的內存消耗增加了多少!   
  22.         cout<<"Size of stream = "<<stream.str().length()<<endl;  
  23.         system("PAUSE");  
  24.     }  
  25.   
  26.     system("PAUSE");  
  27.     return EXIT_SUCCESS;  
  28. }  

由於stringstream構造函數會特別消耗內存,似乎不打算主動釋放內存(或許是爲了提高效率),但如果你要在程序中用同一個流,反覆讀寫大量的數據,將會造成大量的內存消耗,因些這時候,需要適時地清除一下緩衝 (用 stream.str("") )。

另外不要企圖用 stream.str().resize(0),或 stream.str().clear() 來清除緩衝,使用它們似乎可以讓stringstream的內存消耗不要增長得那麼快,但仍然不能達到清除stringstream緩衝的效果,內存的消耗還在緩慢的增長!,至於stream.flush(),則根本就起不到任何作用。


//=============================================

究竟什麼情況下需要用到clear

先來看一個stack overflow上的問題(http://stackoverflow.com/questions/35080342/using-two-string-streams-but-not-getting-same-result

我將其簡化爲以下代碼:

int main() {
    string line = "1 2 3 4 5";
    stringstream s1(line);
    string temp;
    int toAdd;
    stringstream s2;
    while (s1 >> temp) {
        cout << "temp:" << temp << endl;
        s2 << temp;
        cout << "s2.str: " << s2.str() << endl;
        s2 >> toAdd;
        cout << "toAdd:" << toAdd << endl;
        s2.str("");
    }
    return 0;
}

這個代碼的原意是要把line中的字符串形式的1 2 3 4 5一個一個地轉成int並輸出,所以我們期望的toAdd的輸出應該是1 2 3 4 5,但結果卻是 1 1 1 1 1, 如下圖

 

可以從s2.str:這句輸出中看到, 只有第一次是正常地把temp輸入進s2,後面的都失敗了。

原因在於, s2在第一次調用完operator<<和operator>>後,來到了end-of-file的位置,此時stringstream會爲其設置一個eofbit的標記位,標記其爲已經到達eof。查文檔得知, 當stringstream設置了eofbit,任何讀取eof的操作都會失敗,同時,會設置failbit的標記位,標記爲失敗狀態。所以後面的操作都失敗了,toAdd的值一直都是1。

Operations that attempt to read at the End-of-File fail, and thus both the eofbit and the failbit end up set. This function can be used to check whether the failure is due to reaching the End-of-File or to some other reason.

 

clear函數: 

原型: void clear (iostate state = goodbit);

標誌位一共有4種, goodbit, eofbit, failbit, badbit

clear可以清除掉所有的error state

int main() {
    string line = "1 2 3 4 5";
    stringstream s1(line);
    string temp;
    int toAdd;
    stringstream s2;
    while (s1 >> temp) {
        cout << "temp:" << temp << endl;
        s2 << temp;
        cout << "s2.str: " << s2.str() << endl;
        s2 >> toAdd;
        cout << "toAdd:" << toAdd << endl;
        s2.str("");
        if (s2.eof()) {
            s2.clear();
            cout << "s2.eof true" << endl;
        }
    }
    return 0;
}

使用clear後, s2就可以正常地工作了,結果如下:  

 

參考網站:http://www.cplusplus.com/reference/sstream/stringstream/

       http://www.cplusplus.com/reference/ios/ios/clear/


轉自這個地方這個地方

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章