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


相关推荐

  • pyharm激活码_通用破解码

    pyharm激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    51
  • 第三章 语义陷阱

    第三章 语义陷阱

    2022年1月10日
    32
  • MATLAB函数拟合使用

    MATLAB函数拟合使用1函数命令拟合最常用的函数拟合命令为fit,语法为|[拟合结果拟合精度]=fit(X数据,Y数据,‘拟合类型’)其中,具体的拟合类型可以参看帮助文档,也可以使用fittype来自定义新的函数类型,比如定义拟合函数a*x+b*x^2+exp(4*x);|newtype=fittype(‘a*x+b*x^2+exp(4*x)’);fit(x,y,newtype);x=[1;2;…

    2022年7月17日
    12
  • 红模仿在说谁(评价红模仿)

    各位读者,知识无穷而人力有穷,所以,要么改需求,要么找专业人士,要么自己研究。大家可以点赞、收藏、关注、评论我啦、需要完整文件随时联系我或交流哟~!

    2022年4月12日
    52
  • jdbc批量插入实例

    jdbc批量插入实例

    2021年7月16日
    69
  • kafka 集群搭建步骤

    kafka 集群搭建步骤  kafka集群的运行建立在zookeeper基础上,kafka中的消息偏移量信息、kafka集群信息、topic等信息会被存储在zookeeper中。高版本的kafka自带了zookeeper,本文使用独立的zookeeper集群部署kafka集群。前期准备见《Linux环境下,搭建ZooKeeper集群》。一、下载kafka  kafka官网:http://kafka.apache.org/http://kafka.apache.org/downloa

    2022年4月30日
    41

发表回复

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

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