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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • vs2021安装教程_vs2015企业版安装教程

    vs2021安装教程_vs2015企业版安装教程一、学习C/C++语言用什么环境?DevC++很久不在更新,对于C语言的标准支持不够好vc6.0太古老,98年的软件了二、那小编推荐什么大家用什么软件?VS2013及以上版本都行!今天跟大家唠如何安装VS2022(听说VS官网更新了,小伙伴们找不到VS2019了)三、VS2019下载官网VS2022官网https://visualstudio.microsoft.com/zh-hans/?rr=https://www.microsoft.com/zh-cn/3.1点击VS

    2022年9月10日
    0
  • CentOS7修改IP地址后无法ping通问题记录[通俗易懂]

    CentOS7修改IP地址后无法ping通问题记录在键入ifconfigenp0s3192.168.56.2后,在cmd中无法ping通此IP地址解决办法:键入sudoifconfigenp0s3192.168.56.2

    2022年4月12日
    92
  • Json 数组拼接「建议收藏」

    Json 数组拼接「建议收藏」varstr1={“name”:”apple”,”sex”:”21″};//参数:prop=属性,val=值functioncreateJson(prop,val){//如果val被忽略if(typeofval===”undefined”){//删除属性…

    2022年5月9日
    90
  • 服务网关配置_服务网关作用

    服务网关配置_服务网关作用目录第一章Gateway介绍1.1、什么是Gateway1.2、为啥用Gateway第二章Gateway三大核心第三章Gateway工作流程第四章Gateway路由功能4.1、项目准备与启动4.2、工程搭建与测试4.3、配置自定义路由4.4、禁止默认的路由4.5、配置动态的路由4.6、用代码来配路由4.7、为下章节做准备第五章Gateway断言功能5.1、谓词:Path5.2、谓词:After5.3、谓词:Before5.4、谓词:Between5.5、谓词:Cookie5.6、谓词:Header

    2022年10月10日
    0
  • Java程序设计(高级及专题)- GUI「建议收藏」

    Java程序设计(高级及专题)- GUI「建议收藏」Java程序设计(高级及专题)- GUI

    2022年4月22日
    46
  • Java栈实现[通俗易懂]

    Java栈实现[通俗易懂]数组实现的栈一:优点:插入和删除很快,缺点:长度有限publicclassStack{ privateinttop=-1; privateObject[]objs; publicStack()throwsException{ this(10); } publicStack(intcapacity)throwsExceptio

    2022年9月6日
    3

发表回复

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

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