Conversion of various data types using C ++ StringStream

zhaozj2021-02-16  55

C

The library defines three classes: IstringStream, OstringStream, and Stringstream, which are used to perform stream input, output, and input and output operations. With these three types of input, the output operation can be easily converted to data types.

Use the input operation to change the data type IstringStream Stream; string result = "123456"; int N = 0; stream << result; // Enter stream >> n; // output to INTN from the string is equal to 123456

You can use a template to make it more calls universal

Template

DataType CEXP (Const Char * LPSZ) {DATATYPE RET; Assert (LPSZ! = Null); const st: string str (lps); std :: istringstream istr (STR); istr >> Ret; Return Ret;}

Use the output operation to convert various types to String the following is the implementation template template

String CSTR (Const Dattype & Data) {std :: ostringstream ostr; ostr.str ();}

Use example: int i = 0; i = CEXP

("1234"); cout << "i ==" << i << endl;

String strconv; strConv = cstr (1234);

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

New Post(0)