I, Itoa () and Atoi ()
Note: These two functions are not standard C functions, but are specific to Windows environments.
1, Itoa
#include <iostream>#include<string>using namespacestd;intMain () {intnum=12345; stringstr; Chars[ -]; Itoa (Num,s,Ten); STR=str+s; cout<<str; return 0;}
Note here: Use Itoa is sure to use the char[] over, if the direct use of string, it may be because there is no early application of memory reasons, although debugging time has a value, but the actual output is not value.
The following code does not have any output.
#include <iostream>#include<string>using namespacestd;intMain () {intnum=12345; stringstr; //Char s[30]; strings; Itoa (num, (Char*) S.c_str (),Ten); STR=str+s; cout<<str; return 0;}
2, Atoi
#include <iostream>#include<string>usingnamespace std; int Main () { int num; string str="12345"; num=atoi (str.c_str ()); cout<<num; return 0 ;}
Second, StringStream
Consider StringStream as a universal type that can swallow any type of data or output any type of data based on the target type.
Note: 1, use is included header file Sstream
2. When using the same StringStream variable more than once, use the. Clear () method to clear the middle.
#include <iostream>#include<string>#include<sstream>using namespacestd;intMain () {intnum_in=12345; stringstr_in="45678"; stringstr_out; stringnum_out; StringStream SS; SS<<num_in; SS>>str_out; Ss.clear (); SS<<str_in; SS>>num_out; cout<<str_out<<num_out; return 0;}
int string to convert each other