• istringstream、ostringstream、stringstream类介绍


    本文转载自: http://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html

    一、C++的输入输出分为三种:

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

        

    (2)基于文件的I/O

            

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

             

    二、头文件

    #include <sstream>

    三、作用

    istringstream类用于执行C++风格的字符串流的输入操作。 

    ostringstream类用于执行C++风格的字符串流的输出操作。 

    strstream类同时可以支持C++风格的串流的输入输出操作。

    四、具体分析

    istringstream类

    描述:从流中提取数据,支持 >> 操作

    这里字符串可以包括多个单词,单词之间使用空格分开

    1 istringstream的构造函数
    2 istringstream::istringstream(string str); 

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

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

    使用:我们可以使用分解点获取不同的数据,完成 字符串其他类型 的转换

    常用成员函数:

    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类

    描述:把其他类型的数据写入流(往流中写入数据),支持<<操作

    ostringstream的构造函数原形:  
    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类的综合,支持<<, >>操作符,可以进行字符串到其它类型的快速转换

    stringstream的构造函数原形如下:  
    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(),则根本就起不到任何作用。

     
  • 相关阅读:
    django email用法
    django ImageField用法
    django集成微博内容
    python读写zip文件
    读写文件
    404渲染
    bzoj1297 / P4159 [SCOI2009]迷路
    bzoj1009 / P3193 [HNOI2008]GT考试
    poj2115 C Looooops(exgcd)
    bzoj1407 / P2421 [NOI2002]荒岛野人(exgcd)
  • 原文地址:https://www.cnblogs.com/SarahZhang0104/p/5775720.html
Copyright © 2020-2023  润新知