Conversion between C ++ int and string types

Source: Internet
Author: User

I haven't written C ++ for a long timeProgramNow, I feel a lot unfamiliar. It takes a long time to convert an int and a string. To facilitate future work, write them down for reference.

Convert 1.int to string

A. Use the stringstream object:

 
1: # include <iostream>
 
2: # include <String>
 
3: # include <sstream>
 
4:Using NamespaceSTD;
 
5:IntMain (){
 
6: stringstream SS;
 
7:IntResult = 1000;
 
8:StringSTR;
 
9: SS <result;
 
10: SS> STR;
11: cout <STR <Endl;
 
12:Return0;
 
13 :}
 
B. Use the sprintf function. The function prototype is int sprintf (char * buffer, const char * Format [, argument ]...).
 
The following string is stored in the buffer, and the format is similar to the printf function. The following optional parameter is the parameter to be converted, for example:
 
1: # include <iostream>
 
2: # include <String>
 
3: # include <sstream>
 
4:Using NamespaceSTD;
 
5:IntMain (){
 
6:IntResult = 1000;
 
7:CharBuffer [100];
8: sprintf (buffer ,"% D", Result );
 
9:StringSTR (buffer );
 
10: cout <STR <Endl;
 
11:Return0;

12 :}

C. Use the ITOA function, which is similar to the sprintf function. The function prototype is char * _ ITOA (INT value, char * buffer, int Radix ),

Value indicates the integer to be converted. buffer stores the converted string. Radix indicates the base number, that is, the base number, which is to be converted to hexadecimal notation. The example is as follows:

 
1: # include <iostream>
 
2: # include <String>
 
3: # include <sstream>
 
4:Using NamespaceSTD;
 
5:IntMain (){
6:IntResult = 1000;
 
7:CharBuffer [100];
 
8: ITOA (result, buffer, 10 );
 
9:StringSTR (buffer );
 
10: cout <STR <Endl;
 
11:Return0;

12 :}

D. Use macro definition. This method is flexible and can be used to convert data of other data types, for example:

1: # include <iostream>
2: # include <String>
3: # define tostring (x) # x
4:Using NamespaceSTD;
5:IntMain (){
6:IntResult = 1000;
7:StringSTR = tostring (result );
8: cout <STR <Endl;
9:Return0;
10 :}

All the above outputs are 1000

2. Convert string to int

A. Use the stringstream object. The method of conversion from int to string is similar. The example is as follows:

 
1: # include <iostream>
2: # include <String>
 
3: # include <sstream>
 
4:Using NamespaceSTD;
 
5:IntMain (){
 
6: stringstream SS;
 
7:StringSTR ="1000";
 
8:IntResult;
 
9: SS <STR;
 
10: SS> result;
 
11: cout <result <Endl;
 
12:Return0;

13 :}

B. Use the atoi function. The function prototype is int atoi (const char * Str). Before using this function, call the string object to the c_str () function,

Example:

1: # include <iostream>
2: # include <String>
3:Using NamespaceSTD;
4:IntMain (){
5:StringSTR ="1000";
6:Const Char* Pstr = Str. c_str ();
7:IntResult = atoi (pstr );
8: cout <result <Endl;
9:Return0;
10 :}

 

All the above outputs are 1000.

all the sample programs in this article have passed the test under vs2008.

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.