c++中 string 和 int 类型转换

本文详细介绍了在C++中如何进行string到int以及int到string的类型转换。主要内容包括使用.c_str()函数将int转换为const char*,然后通过atoi()函数将const char*转换为int。同时,文章提到了转换过程中的注意事项,如包含相应的头文件,并指出不同的编译器可能有不同的实现方式。
摘要由CSDN通过智能技术生成

一、int 类型转换为 string 类型

 示例:

#include <iostream>
#include <string>
 
int main() 
{
    double f = 23.43;
    double f2 = 1e-9;
    double f3 = 1e40;
    double f4 = 1e-40;
    double f5 = 123456789;
    std::string f_str = std::to_string(f);
    std::string f_str2 = std::to_string(f2); // 注意:返回 "0.000000"
    std::string f_str3 = std::to_string(f3); // 注意:不返回 "1e+40".
    std::string f_str4 = std::to_string(f4); // 注意:返回 "0.000000"
    std::string f_str5 = std::to_string(f5);
    std::cout << "std::cout: " << f << '\n'
              <&l
  • 10
    点赞
  • 95
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值