html数字转换成字符串,to_string函数(将数字转换成字符串)

一般常用的转换字符串的方法std::stringstream,但是效率较低;目前C ++11提供了std::to_string

效率方面:C风格的sprintf()没有动态分配内存效率最高;std::to_string其次;std::stringstream效率最差

从C++17开始,std::to_string的效率将不差于sprintf,同时有类型更安全的转换函数std::to_char。

函数重载原型:

l w string to_string(int val);

l w string to_string(long val);

l w string to_string(long long val);

l w string to_string(unsigned val);

l w string to_string(unsigned long val);

l w string to_string(unsigned long long val);

l w string to_string(float val);

l w string to_string(double val);

l w string to_string(long double val);

1 1.#include 2 2.#include  //std::to_string

3 3.#include  //std::stringstream

4 4.intmain()5 5.{6 6.    //old method

7 7.    std::stringstream ss;8 8.    ss <

12 12.    std::string pi = "pi is" + std::to_string(3.1415926);13 13.    std::string perfect = std::to_string(1 + 2 + 4 + 7 + 14) + "is a perfect number";14 14.    std::cout <

1.#include 

2.#include  // std::to_string

3.#include  // std::stringstream

4.intmain()

5.{

6.// old method

7.std::stringstream ss;

8.ss <

9.std::string str = ss.str();

10.std::cout <

11.// new method

12.std::string pi ="pi is"+std::to_string(3.1415926);

13.std::string perfect = std::to_string(1 + 2 + 4 + 7 + 14) +"is a perfect number";

14.std::cout <

15.std::cout <

16.return0;

17.}

原文:https://www.cnblogs.com/pacino12134/p/11054488.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值