In C + +, the conversion of int and string types is encountered.
string-int
First we'll look at two functions:
Atoi
This function converts a char * to an int. Should belong to the standard library function. The following process is required when you want to convert a string to int:
stringcharint
So talented, examples such as the following:
string"1234";int b = atoi(a.c_str());
This is the time to print B 1234 .
Itoa
This function does not seem to belong to the library function when I search, it is function int->char * , so can:
int1234;stringnewstring(itoa(a));
Just have to have itoa function enough.
string int
Here I would like to use the method is stringstream very useful.
Directly above the sample code:
int1234str = ss.str();
This is OK ~ is not very convenient.
It is important to note that the stringstream needs to be emptied before multiple operations. The empty operation is: ss.str("") , no ss.clear() . Remember.
Above.
C + + learning-conversion between int and string