#include<iostream>
#include <string>
#include <sstream>//头文件必须有
using namespace std;
int main(int argc, char const *argv[]) {
//方法一:使用stringstream进行转换,适用于string类型
string st1="123456";
int i;
stringstream ss;
ss<<st1;
ss>>i;//字符转数字的步骤,对float和double同样试用
cout<<"i="<<i<<endl;
string st2;
int j=123456;
stringstream aa;
aa<<j;
aa>>st2;//数字转字符的步骤
cout<<"st2="<<st2<<endl;
//方法二:使用sprintf函数进行转换,适用于char类型,数字转char
char st3[10];
int k=123456;
sprintf(st3,"%d",k);
cout<<"st3="<<st3<<endl;
//方法三:使用sscanf函数进行转换,适用于char类型,char转数字
char st4[10]="123456";
int l;
sscanf(st4,"%d",&l);
cout<<"l="<<l<<endl;
//方法四;使用stoi函数来写
string st5="123456";
int m=stoi(st5);
cout<<"m="<<m<<endl;
return 0;
}
0.0分
0 人评分