ostringstream的使用

ostringstream的使用在std中还有个ostringstream这么方便的格式化输出的工具 std::stringgenerate(){ostringstreamosm;osm<<"xxxx"<<1111;returnosm.str();}

大家好,又见面了,我是你们的朋友全栈君。

在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) 

 

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150233.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月18日 下午5:36
下一篇 2022年6月18日 下午5:36


相关推荐

  • 淘宝,天猫,京东,苏宁抢购茅台、手机等脚本(适合兼职,亲测可用)「建议收藏」

    淘宝,天猫,京东,苏宁抢购茅台、手机等脚本(适合兼职,亲测可用)「建议收藏」由来:最近用钱比较多,缺钱,自己也是除了一份死工资就没有别的收入了,而且每个月的工资发了就立马还给了马爸爸,所以一直在想是不是工作之余搞点什么兼职做做,但是一直也是找不到什么门路。碰巧看到uc给我推了一个文章(其实一直有推只是之前没有想法每注意),说天猫,京东,苏宁这些网购平台的茅台90%都是买来卖的,一瓶利润能有一千块。我顿时有了想法,就想着能不能搞个脚本啥的去抢抢,总比自己手动来得好,然后就上网去找脚本,修修补补改改,基于别人的基础上搞了个脚本(目前只有安卓版,后续可能会出苹果的)。然后坚持了几天,

    2022年5月16日
    106
  • python shapely.geometry.polygon 实现 任意两个四边形的IOU计算

    python shapely.geometry.polygon 实现 任意两个四边形的IOU计算在目标检测中一个很重要的问题就是 NMS 及 IOU 计算 而一般所说的目标检测检测的 box 是规则矩形框 计算 IOU 也非常简单 有两种方法 1 两个矩形的宽之和减去组合后的矩形的宽就是重叠矩形的宽 同比重叠矩形的高 2 右下角的 minx 减去左上角的 maxx 就是重叠矩形的宽 同比高然后 IOU 重叠面积 两矩形面积和 重叠面积 nbsp 然 不规则四边形就不能通过这种方式来计算 找了

    2026年3月17日
    2
  • smartctl命令详解_fuser命令

    smartctl命令详解_fuser命令smart用法1.smartctl-son/dev/sda”开启磁盘的smart功能2.smartctl-a/dev/sda”显示磁盘的详细smart信息3.smartctl–test=short/dev/sda4.smartctl–test=short/dev/sdb检查磁盘坏道…

    2022年10月8日
    10
  • msiexec用法详解

    msiexec用法详解在系统管理中,尤其是静默安装和卸载软件的时候,该命令就会经常用到,详细用户如下:(本文拷贝自微软网站)msiexec/Option<RequiredParameter>[OptionalParameter]安装选项 </package|/i><Product.msi> 安装或配置产品 /a<Product.msi&…

    2025年7月25日
    6
  • 关于HTTP请求出现 405状态码 not allowed的解决办法

    关于HTTP请求出现 405状态码 not allowed的解决办法

    2021年10月12日
    58
  • Stimulsoft Ultimate Reports 2022.2.6

    Stimulsoft Ultimate Reports 2022.2.6Publishedon27May2022

    2022年7月26日
    19

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号