#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 人评分
C二级辅导-同因查找 (C语言代码)浏览:700 |
C语言程序设计教程(第三版)课后习题11.3 (C语言代码)浏览:1061 |
【密码】 (C语言代码)浏览:345 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:692 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:778 |
C语言程序设计教程(第三版)课后习题8.1 (C语言代码)浏览:562 |
printf基础练习2 (C语言代码)浏览:648 |
C语言程序设计教程(第三版)课后习题10.3 (C语言代码)浏览:515 |
C二级辅导-分段函数 (C语言代码)浏览:653 |
C语言程序设计教程(第三版)课后习题10.7 (用指针求解)浏览:1531 |