乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      istringstream、ostringstream、stringstream 類介紹 .

       ONLY_影 2015-02-06

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

      (1)基于控制臺的I/O

         

      (2)基于文件的I/O

              

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

                

      1、頭文件

      [cpp] view plaincopyprint?

      1. #include <sstream>  

      2、作用

      istringstream類用于執(zhí)行C++風(fēng)格的字符串流的輸入操作。 

      ostringstream類用于執(zhí)行C++風(fēng)格的字符串流的輸出操作。 

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

      3、具體分析

      istringstream類

      描述:從流中提取數(shù)據(jù),支持 >> 操作

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

      1. istringstream的構(gòu)造函數(shù)原形:  
      2. istringstream::istringstream(string str);  

      初始化:使用字符串進(jìn)行初始化

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

      使用:我們可以使用分解點獲取不同的數(shù)據(jù),完成 字符串 到 其他類型 的轉(zhuǎn)換

      常用成員函數(shù):

       

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

      舉例:把字符串類型的數(shù)據(jù)轉(zhuǎn)換為其他類型

       

      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;//直接輸出字符串的數(shù)據(jù) "1 56.7"   
      9.       
      10.     string str = istr.str();//函數(shù)str()返回一個字符串   
      11.     cout<<str<<endl;  
      12.       
      13.     int n;  
      14.     double d;  
      15.   
      16.     //以空格為界,把istringstream中數(shù)據(jù)取出,應(yīng)進(jìn)行類型轉(zhuǎn)換   
      17.     istr>>n;//第一個數(shù)為整型數(shù)據(jù),輸出1   
      18.     istr>>d;//第二個數(shù)位浮點數(shù),輸出56.7   
      19.   
      20.     //假設(shè)換下存儲類型   
      21.     istr>>d;//istringstream第一個數(shù)要自動變成浮點型,輸出仍為1   
      22.     istr>>n;//istringstream第二個數(shù)要自動變成整型,有數(shù)字的階段,輸出為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類

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

      1. ostringstream的構(gòu)造函數(shù)原形:  
      2. ostringstream::ostringstream(string str);  

      初始化:使用字符串進(jìn)行初始化

      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類的綜合,支持<<, >>操作符,可以進(jìn)行字符串到其它類型的快速轉(zhuǎn)換

      1. stringstream的構(gòu)造函數(shù)原形如下:  
      2. stringstream::stringstream(string str);  

      初始化:使用字符串進(jìn)行初始化

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

      作用:

      1、stringstream通常是用來做數(shù)據(jù)轉(zhuǎn)換的

      2、將文件的所有數(shù)據(jù)一次性讀入內(nèi)存

      舉例1:基本數(shù)據(jù)類型變字符串

      1. /*基本數(shù)據(jù)類型變字符串*/  
      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:字符串變基本數(shù)據(jù)類型

      1. /*字符串變基本數(shù)據(jù)類型*/  
      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(),這個名字讓很多人想當(dāng)然地認(rèn)為它會清除流的內(nèi)容。   
      12.         //實際上,它并不清空任何內(nèi)容,它只是重置了流的狀態(tài)標(biāo)志而已!   
      13.         stream.clear();    
      14.   
      15.         // 去掉下面這行注釋,清空stringstream的緩沖,每次循環(huán)內(nèi)存消耗將不再增加!   
      16.         //stream.str("");         
      17.   
      18.         stream<<"sdfsdfdsfsadfsdafsdfsdgsdgsdgsadgdsgsdagasdgsdagsadgsdgsgdsagsadgs";  
      19.         stream>>str;     
      20.   
      21.         //測試輸出每次循環(huán),你的內(nèi)存消耗增加了多少!   
      22.         cout<<"Size of stream = "<<stream.str().length()<<endl;  
      23.         system("PAUSE");  
      24.     }  
      25.   
      26.     system("PAUSE");  
      27.     return EXIT_SUCCESS;  
      28. }  

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

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

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多