🙏
#include<iostream>
using namespace std;
int main()
{
//1:stoi函数会截取整数部分转化为int型
//2:stoi在截取过程中遇到不是数字字符,直接停止截数
//3:stoi如果数字字符串前面为空格会直接跳过空格进行截数
//4: stoi如果数字字符串前面有除空格外的其他字符会报错
string s=" 1122 33.1111";
printf("stoi截取:%d\n",stoi(s));
s=" 112233.1111";
printf("stoi截取:%d\n",stoi(s));
cout<<endl;
//1:stod函数,将数字字符串转化为double型
//2:stod函数,在截取过程中遇到非法字符(包括空格)停止截数
//3:stod函数,数字字符串前面有空格直接会跳过空格进行截数
//4:stod函数,转化为double型如果小数点后面不满6位,会自动补0
string s1=" 1234.5678";
double a=stod(s1);
printf("stod截取:%lf\n",a);
s1=" 123 4.567";
a=stod(s1);
printf("stod截取:%lf\n",a);
}
运行结果:
stoi截取:1122
stoi截取:112233
stod截取:1234.567800
stod截取:123.000000