C + + / C -- Introduction to istringstream, ostringstream and stringstream [reprint]

Posted by bullbreed on Sun, 28 Jun 2020 04:35:28 +0200

Article catalog

1 the input and output of C + + are divided into three types:

1.1 console based I/O

1.2 file based I/O

1.3 string based I/O

2 header file

#include <sstream>  

3 functions

  • The istringstream class is used to perform input operations for C + + style string streams.

  • The ostringstream class is used to perform output operations of C + + style string streams.

  • strstream class can also support C + + style stream input and output operations.

4. Specific analysis

4.1 istringstream class

Description: extract data from flow, support > > operation

Here the string can contain multiple words, separated by spaces

istringstream Constructor primitive for:  
istringstream::istringstream(string str);  

Initialization: using strings for initialization

istringstream istr("1 56.7");  
istr.str("1 56.7");//Store string "1 56.7" in string stream   

Use: we can use decomposition point to get different data and complete string to other types of conversion

Common member functions:
str(): causes the istringstream object to return a string string

Example: converting data of string type to other types

#include <iostream>   
#include <sstream>   
using namespace std;  
int main()  
{  
    istringstream istr("1 56.7");  
  
    cout<<istr.str()<<endl;//Direct output string data "1 56.7"   
      
    string str = istr.str();//Function str() returns a string   
    cout<<str<<endl;  
      
    int n;  
    double d;  
  
    //The data in istringstream should be fetched out with space as the boundary, and the type conversion should be carried out   
    istr>>n;//The first number is integer data, output 1   
    istr>>d;//Second digit floating-point number, output 56.7   
  
    //Suppose you change the storage type   
    istr>>d;//The first number of istringstream will automatically become floating-point, and the output will still be 1   
    istr>>n;//The second number of istringstream will automatically become an integer type. In the digital stage, the output is 56   
  
    //Test output   
    cout<<d<<endl;  
    cout<<n<<endl;  
    system("pause");  
    return 1;  
}  

Example 2: put a line of strings into the stream, with words separated by spaces. Then read the words from the stream to the string one by one

#include <iostream>   
#include <sstream>   
using namespace std;  
int main()  
{  
    istringstream istr;  
    string line,str;  
    while (getline(cin,line))//Receive a line of string from the terminal and put it into the string line   
    {  
        istr.str(line);//Store string in line into string stream   
        while(istr >> str)//Read one word at a time (bounded by spaces) and store it in str   
        {  
            cout<<str<<endl;  
        }  
    }  
    system("pause");  
    return 1;  
}  

Input: 123 34 45

Output:

123newline 34newline 45

4.2 ostringstream class

Description: write other types of data to the stream (write data to the stream), and support < operation

ostringstream Constructor primitive for:  
ostringstream::ostringstream(string str);  

Initialization: using strings for initialization

ostringstream ostr("1234");  
ostr.str("1234");//Store string "1234" in string stream  

give an example:

#include <iostream>   
#include <sstream>   
using namespace std;  
int main()  
{  
    //Initialize output string stream ostr   
    ostringstream ostr("1234");  
    cout<<ostr.str()<<endl;//Output 1234   
      
    ostr.put('5');//Character 4 replaces position 1   
    cout<<ostr.str()<<endl;//Output 5234   
  
    ostr<<"67";//String 67 replaces position 23, output 5674   
    string str = ostr.str();  
    cout<<str<<endl;  
    system("pause");  
    return 1;  
}  

4.3 stringstream class

Description: it is a synthesis of istringstream and ostringstream classes. It supports the operators of < < and > > and can quickly convert strings to other types

stringstream The constructor primitive for is as follows:  
stringstream::stringstream(string str);  

Initialization: using strings for initialization

stringstream str("1234");  
str.str("1234");//Store string "1234" in string stream 

effect:

1. stringstream is usually used for data conversion

2. Read all data of the file into memory at one time

Example 1: basic data type changing string

/*Basic data type variable string*/  
#include <fstream>   
#include <iostream>   
#include <sstream>   
using namespace std;  
int main()  
{  
    /*Integer variable string*/  
    int n = 10;  
    string str;  
    stringstream stream;  
      
    stream << n;  
    stream >> str;  
      
    cout<<str<<endl;  
    stream.clear();//If stringstream is used multiple times, it should be emptied first. It cannot be used stream.str(""); otherwise output 10 below   
  
    /*char* Change string*/  
    char cStr[10] = "china";  
      
    stream << cStr;  
    stream >> str;  
  
    cout<<str<<endl;  
    system("pause");  
    return 1;  
}  

Example 2: String changing basic data type

/*String to basic data type*/  
#include <fstream>   
#include <iostream>   
#include <sstream>   
using namespace std;  
int main()  
{  
    /*String change double*/  
    double n;  
    string str = "12.5";  
    stringstream stream;  
      
    stream << str;  
    stream >> n;  
      
    cout<<n<<endl;  
    stream.clear();//If stringstream is used multiple times, it should be emptied first. It cannot be used stream.str("");   
  
    /*string Change char* */  
    string str1 = "china";  
    char cStr[10];  
      
    stream << str1;  
    stream >> cStr;  
  
    cout<<cStr<<endl;//Output china   
    system("pause");  
    return 1;  
}  

be careful:

#include <iostream>   
#include <sstream>   
using namespace std;  
  
int main(int argc,char *argv[])  
{  
    std::stringstream stream;  
    string str;  
    while(1)  
    {     
        //The name clear() makes many people assume that it will clear the content of the stream.   
        //In fact, it doesn't empty anything, it just resets the status flag of the stream!   
        stream.clear();    
  
        // Remove the following line of comments, clear the buffer of stringstream, and the memory consumption of each cycle will not increase!   
        //stream.str("");         
  
        stream<<"sdfsdfdsfsadfsdafsdfsdgsdgsdgsadgdsgsdagasdgsdagsadgsdgsgdsagsadgs";  
        stream>>str;     
  
        //Test how much more memory you consume per cycle of output!   
        cout<<"Size of stream = "<<stream.str().length()<<endl;  
        system("PAUSE");  
    }  
  
    system("PAUSE");  
    return EXIT_SUCCESS;  
}  

Because the stringstream constructor consumes memory in particular, it seems that it is not intended to actively release memory (perhaps to improve efficiency), but if you want to use the same stream in the program and repeatedly read and write a large amount of data, it will cause a large amount of memory consumption, so you need to clear the buffer in time stream.str("") ).

And don't try to use it stream.str().resize(0), or stream.str().clear() to clear the buffer. It seems that using them can make the memory consumption of stringstream not grow so fast, but still can not achieve the effect of clearing the buffer of stringstream. The memory consumption is still growing slowly! , as for stream.flush(), it doesn't work at all.

From:

  1. Cold night online game programming technology_ Introduction to istringstream, ostringstream and stringstream classes. [CNBLOGS]

Topics: Programming