Use the StringStream object to implement the conversion between data types

zhaozj2021-02-16  69

Many people use traditional C

The reservoir is converted between data types, which will result in many problems because such a conversion method has many dangerous traps. For example, the ITOA () This function does not exist in the standard library. standard

The library provides a better conversion option because this method is safer, automatically, directly.

Let us look at a specific example. Suppose you want to convert an int to String. In order to achieve this, you must follow the steps:

1. Create a Stringstream object,

2. Use the operator << Insert int data,

3. Use operator >> Patting the previously inserted data into a String object.

The following code line demonstrates these steps:

// Program Name: TestStream.cpp // Function: Transfer int type data into String through the StringStream object

#include

#include

// system ()

#include

#include

Using namespace std;

INT Main (int Argc, char * argv []) {std :: stringstream stream; std :: string result; int num = 1000; stream << Num; // Insert int type data into Stream >> Result; // Remove the previously inserted data cout << "Num: / t" << Num << endl; coult << "result: / t" << result << Endl; // Print "1000" System ("Pause" RETURN 0;}

Please note that we do not use a simple CAST operation or a pattern flag to implement StringStream conversion. Operators << and >> automatically delete the type and target data of the raw data, and automatically perform the required conversions automatically.

The library will not be limited to some high levels of operation, such as std :: string. You can easily implement a conversion between a char * variable:

// Program Name: TestStream2.cpp // Function: Transfer Int type data into char [] #include

#include

// system ()

#include

#include

Using namespace std;

Int main (int Argc, char * argv []) {std :: stringstream stream; char result [12] = {'/ 0'}; stream << 1234; // Insert Int to stream stream >> Result; // Extract previously INSERTED VALUE COUT << Result << Endl; // print "1234" System ("pause"); return 0;} If you want to implement multiple types of conversions by using the same StringStream object, please pay attention to each conversion After that, you must call the CLEAR () member function, for example:

// Program Name: TestStream3.cpp // Function: Use the same StringStream object to implement multiple types of conversion #include

#include

// system ()

#include

#include

Using namespace std;

INT Main (int Argc, char * argv []) {std :: stringstream stream; int N, m; stream << "456"; // insert string stream >> n; // extract to int stream.clear () ; // reset stream before another conversion stream << true; // INSERT BOOL VALUE stream >> m; // extract to int coupter << "N: / t" << n << endl; //print 456 cout < <"m: / t" << m << endl; // print 1 system ("pause"); return 0;}

In fact, the Stream object can receive a variety of types of inputs to bring us a benefit, which can simultaneously import different types of inputs such as int, char * simultaneously into a Stream object, and then export a new value through the Stream object.

// Program Name: TestStream4.cpp // Function: Transfer int type data and char * data to CHAR []

#include

#include

// system ()

#include

#include

Using namespace std;

转载请注明原文地址:https://www.9cbs.com/read-18832.html

New Post(0)