[Reprinted] Common usage of stringstream class in C ++

This article mainly introduces the common usage of the stringstream class in C ++.

1 Overview

<sstream> defines three classes: istringstream, ostringstream, and stringstream, which are used for stream input, output, and input and output operations, respectively. This article focuses on stringstream, introducing stream input and output operations.

<sstream> is mainly used for data type conversion. Because <sstream> uses string objects instead of character arrays (snprintf method), it avoids the danger of buffer overflow; moreover, because the parameters and target object types are automatically derived Out, so there is no problem with the wrong formatter. Simply put, compared to the data type conversion of the C library, <sstream> is more secure, automatic, and direct.

2 Code example

2.1 Data type conversion

Here is a code example that introduces the process of converting an int type to a string type. The sample code (stringstream_test1.cpp) is as follows:

1 #include < string >
 2 #include <sstream>
 3 #include <iostream>
 4 #include <stdio.h>
 5   
6  using  namespace std;
 7   
8  int main ()
 9  {
 10      stringstream sstream;
 11      string strResult;
 12      int nValue = 1000 ;
 13   
14      // Put the value of type int into the input stream 
15      sstream << nValue;
 16      // Extract the value of type int inserted earlier from sstream and assign it to the type of string 
17     sstream >> strResult;
18  
19     cout << "[cout]strResult is: " << strResult << endl;
20     printf("[printf]strResult is: %s\n", strResult.c_str());
21  
22     return 0;
23 }

Compile and execute the above code, the results are as follows:

2.2 Multiple string concatenation

This example introduces the storage of multiple strings in stringstream to achieve the purpose of concatenation of multiple strings (in fact, it can be achieved using the string class), and at the same time, introduces the empty method of stringstream.

The sample code (stringstream_test2.cpp) is as follows:

 1 #include <string>
 2 #include <sstream>
 3 #include <iostream>
 4  
 5 using namespace std;
 6  
 7 int main()
 8 {
 9     stringstream sstream;
10  
11     // 将多个字符串放入 sstream 中
12     sstream << "first" << " " << "string,";
13     sstream << " second string";
14     cout << "strResult is: " << sstream.str() << endl;
15  
16     // 清空 sstream
17     sstream.str("");
18     sstream << "third string";
19     cout << "After clear, strResult is: " << sstream.str() << endl;
20  
21     return 0;
22 }

Compile and execute the above code, the results are as follows:

From the above code execution results, we can know:

  • You can use str () method to convert stringstream type to string type;
  • Multiple strings can be put into stringstream to achieve the purpose of string concatenation;
  • If you want to clear the stringstream, you must use the sstream.str (""); method; the clear () method is suitable for scenarios where multiple data type conversions are performed. See example 2.3 for details.

2.3 Emptying of stringstream

There are two methods to clear stringstream: clear () method and str ("") method. These two methods have different usage scenarios. The usage scenarios of the str ("") method have been introduced in the above example. Here are the usage scenarios of the clear () method. The sample code (stringstream_test3.cpp) is as follows:

1 #include <sstream>
 2 #include <iostream>
 3   
4  using  namespace std;
 5   
6  int main ()
 7  {
 8      stringstream sstream;
 9      int first, second;
 10   
11      // Insert string 
12      sstream << " 456 " ;
 13      // Convert to int type 
14      sstream >> first;
 15      cout << first << endl;
 16   
17      // Before performing multiple type conversions, you must first run clear () 
18     sstream.clear ();
 19   
20      // Insert bool value 
21      sstream << true ;
 22      // Convert to int type 
23      sstream >> second;
 24      cout << second << endl;
 25   
26      return  0 ;
 27 }

Compile and execute the above code, the results are as follows:

Note: In the scenario involved in this example (multiple data type conversions), you must use the clear () method to clear the stringstream, without using the clear () method or the str ("") method, you cannot get the correct result of the data type conversion . The following figure shows the running results when the clear () method is not used and the str ("") method is used:

 

The original text is reproduced from https://blog.csdn.net/liitdar/article/details/82598039

Guess you like

Origin www.cnblogs.com/PennyXia/p/12743255.html