Convert int type in C ++ to string type

Source: Internet
Author: User

For C # or Java, various data types can be converted at will, and various functions can be selected at will. But it is not that simple in C ++.

I originally wanted to be able to convert 123 to a string of the int type such as "123" or a C-style.

I carefully checked the available methods of C ++ string:

Of course I know what string is in C ++!

In the official explanation, string is a special container used to store character sets. It is the char implementation of the basic_string template class. The other one is wchar.

TypedefBasic_string <Char> String;

Since string is a container, there are some container operations:

For example, our common size, push_back (); of course there are also begin, end ,.

In addition, we have many methods that include the common processing of C-style strings. However, no function can convert int to string.

So we can only start from another angle, first convert int to a C-style string, and then convert it to a string.

This function basically appears in the C library.

Method 1: In cstdlib, a function, ITOA, is used for this conversion.

 
Char * ITOA (INT value, char * STR, int base );
The associated methods include atof, atoi. atol, but note that these are all functions in the C library. The function in the C library is characteristic that the string uses the C-sytle string.
 
However, in UNIX, there is no such ITOA function (marked as non-stondard above ).
 
So we are only looking at other methods. In cstdio, there are many functions related to input and output (including basic operations on files, in one sentence, the input to the file is the standard input, which requires some basic operations on the file ). But the exception is that one type of function is about input and output of strings -- sprintf, sscanf.
 
Char temp [100];
 
Int Ss = 100;
 
Sprintf (temp, "% d", SS );
This is determined based on the specific conditions of input and output. We know that the most direct expression of input and output is "display", whether it is output to a file, on the screen, or in a string, all appear in the form of strings. Therefore, this method outputs only eight characters of data such as 100 in the memory space, because people can only understand characters.
 
Here we use the features mentioned above.
 
Method 2: Use the sprintf method in cstdio to "display" int data to the string.
 
The second method is to use the input and output methods in the C library. Since C ++ basically writes this method from the stream object form, is there a way to do this using C ++? Yes!
 
Int;
 
Osstream ostr;
 
Ostr <;
String S = ostr. STR ();
 
You can see it directly.
 
Method 3: Use sstream
Related Article

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.