ostringstream的使用

在std中还有个ostringstream这么方便的格式化输出的工具
 

std::string generate()
{
    ostringstream osm;
    osm << "xxxx " << 1111;
    
    // 清空之前的内容
    osm.str("");
    
    // 浮点数转换限制
    double tmp = 123.1234554351236234;
    osm.precision(15);
    osm.setf(std::ios::fixed); // 将浮点数的位数限定为小数点之后的位数
    osm << tmp; 

    return osm.str();
}

需要包含的头文件:

#include <sstream> 

 

ostringstream作用、主要接口:

Output string stream class

ios_base  ios  ostream  ostringstream 

ostringstream provides an interface to manipulate strings as if they were output streams.

The objects of this class maintain internally a pointer to a stringbuf object that can be obtained/modified by calling member rdbuf. This streambuf-derived object controls a sequence of characters (string) that can be obtained/modified by calling member str.


Public members
(constructor) Construct an object and optionally initialize its content (constructor member) 
rdbuf Get/set the associated stringbuf object (public member function) 
str Get/set the associated string object (public member function) 

 

一个非常简单又快捷的格式化输出工具。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值