Converts a C + + numeric type to a string __c++

Source: Internet
Author: User
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main ()
{
 int a =;
 Double b = 65.123;
 String str = "";

 Header file is Sstream
 ostringstream oss;
 OSS << a << "---" << b;

 str = OSS.STR ();
 cout << str << endl;
 return 0;
}
To achieve this goal, the StringStream category Mo genus.

This class is defined in the header file, < Sstream> library defines three kinds: Istringstream, Ostringstream, and StringStream, which are used for flow input, output, and input/output operations respectively. In addition, each class has a corresponding wide version of the character set.

For simplicity, I mainly focus on stringstream, because each transformation involves both input and output operations.

Example 1 demonstrates how to use a StringStream object to convert from string to int types, using a string object instead of a character array. This avoids the risk of a buffer overflow. Also, the types of incoming and target objects are automatically deduced, and there is no danger even if an incorrect format character is used.

Example 1:
Copy code code as follows:
Std::stringstream stream;

String result= "10000";

int n = 0;

Stream << result; Stream >> n;//n equals 10000

int to String type conversions
Copy code code as follows:
string result;

int n = 12345;

Stream << N;

Result =stream.str ()//Result equals "12345"

Reusing StringStream objects if you intend to use the same StringStream object in multiple conversions, remember to use the clear () method before each conversion to reuse the same one in multiple conversions StringStream (instead of creating a new object each time) the biggest benefit is efficiency. The construction and destructors of StringStream objects are often CPU-intensive. On trial, using clear only () does not erase the contents of the StringStream object, only the state of the object, and to reuse the same StringStream object, you need to reinitialize the object with STR ().

Example 2:
Copy code code as follows:
Std::stringstream strSQL;

for (int i= 1; i < ++i)

{

strSQL << "INSERT into test_tab values (";

strSQL << i << "," << (i+10) << ");";

std::string str = STRSQL.STR ();//Get string

res = Sqlite3_exec (Pdb,str.c_str (), 0,0, &errmsg);

Std::cout << strsql.str () << Std::endl; Strsql.clear ();

Strsql.str ("");

}

Using a template in a transformation also makes it easy to define a function template to convert an arbitrary type to a specific target type.

For example, you need to convert various numeric values, such as int, long, double, and so on to strings, to use the to_string () function with a string and an arbitrary value t as a parameter.

The to_string () function converts t to a string and writes to result.

Use the STR () member function to obtain a copy of the flow internal buffer:

Example 3:
Copy code code as follows:
template void To_string (string & result,const t& T)

{Ostringstream oss;//Create a stream oss< Out_type convert (const In_value & T)

{StringStream stream; stream<>result;//writes value to result;

Use CONVERT (): Double D; string salary; String s= "12.56"; D=convert (s);//d equals 12.56 Salary=convert (9000.0);//salary equals "9000"

Conclusion: in the past, the traditional form conversion has been with us for a long time in the program code and pure C program. However, as described in the article, StringStream conversions have type safety and do not overflow such eye-catching features, so that we have ample reason to discard and use < sstream>.

There is, of course, a better option now, using the lexical_cast in the Boost library, which is a type-safe conversion.

The following example:
Copy code code as follows:
#include #include #include #include #include

using namespace Std;

using namespace boost;

int main (void)

Try

{

The following is a solution to convert a built-in type to string

Lexical_cast Advantage is obvious

int ival;

Char Cval;

Ostringstream out_string;

String str0;

String str1;

ival = 100;

Cval = ' W ';

out_string << ival << "" << cval;

STR0 = Out_string.str ();

STR1 = Lexical_cast (ival) + lexical_cast (cval);

cout << str0 << Endl; cout << str1 << Endl;

The following is a resolution of string conversion to a built-in type

Compared with Stringstrem, lexical_cast is of type safety,

int Itmpe;

Char Ctmpe;

STR0 = "100k";

str1 = "100h";

Istringstream in_string (STR0);

in_string >> Itmpe >> Ctmpe;

cout << itmpe << "" << ctmpe << Endl;

Itmpe = Lexical_cast (STR1);

Ctmpe = Lexical_cast (STR1);

System ("PAUSE");

return 0;

catch (Bad_lexical_cast e) {cout << e.what () << Endl; Cin.get ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.