#include#include#includeusing namespace std;int main(int argc, char const *argv[]){ //字符串转数字 1 string str1="1234"; int i; stringstream ss1; ss1<<str1; ss1>>i; cout<<i<<endl; //字符串转数字 2 string str="123"; const char*s = str.c_str(); cout<<atoi(s)<<endl; //数字转字符串 string str2; stringstream ss2; int a=1024; ss2<<a; ss2>>str2; cout<<str2<<endl; cout<<to_string(a)<<endl; return 0;}
注:
- 函数原型:
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);