c++中的stod()与stoi()两大函数的应用

🙏

#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
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值