ostringstream是C++的一个字符集操作模板类,定义在sstream.h头文件中。ostringstream类通常用于执行C风格的串流的输出操作,格式化字符串,避免申请大量的缓冲区,替代sprintf。

派生关系图:

ostringstream
 

ostringstream的构造函数形式:

  1. explicit ostringstream ( openmode which = ios_base::out );
  2. explicit ostringstream ( const string & str, openmode which = ios_base::out );

有时候,我们需要格式化一个字符串,但通常并不知道需要多大的缓冲区。为了保险常常申请大量的缓冲区以防止缓冲区过小造成字符串无法全部存储。这时我们可以考虑使用ostringstream类,该类能够根据内容自动分配内存,并且其对内存的管理也是相当的到位。

  1. #include <sstream>
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5. void main()
  6. {
  7. ostringstream ostr1; // 构造方式1
  8. ostringstream ostr2("abc"); // 构造方式2
  9. /*----------------------------------------------------------------------------
  10. *** 方法str()将缓冲区的内容复制到一个string对象中,并返回
  11. ----------------------------------------------------------------------------*/
  12. ostr1 << "ostr1" << 2012 << endl; // 格式化,此处endl也将格式化进ostr1中
  13. cout << ostr1.str();
  14. /*----------------------------------------------------------------------------
  15. *** 建议:在用put()方法时,先查看当前put pointer的值,防止误写
  16. ----------------------------------------------------------------------------*/
  17. long curPos = ostr2.tellp(); //返回当前插入的索引位置(即put pointer的值),从0开始
  18. cout << "curPos = " << curPos << endl;
  19. ostr2.seekp(2); // 手动设置put pointer的值
  20. ostr2.put('g');     // 在put pointer的位置上写入'g',并将put pointer指向下一个字符位置
  21. cout << ostr2.str() << endl;
  22. /*----------------------------------------------------------------------------
  23. *** 重复使用同一个ostringstream对象时,建议:
  24. *** 1:调用clear()清除当前错误控制状态,其原型为 void clear (iostate state=goodbit);
  25. *** 2:调用str("")将缓冲区清零,清除脏数据
  26. ----------------------------------------------------------------------------*/
  27. ostr2.clear();
  28. ostr2.str("");
  29. cout << ostr2.str() << endl;
  30. ostr2.str("_def");
  31. cout << ostr2.str() << endl;
  32. ostr2 << "gggghh";    // 覆盖原有的数据,并自动增加缓冲区
  33. cout << ostr2.str() << endl;
  34. }

详细用法请参考如下网址:http://www.cplusplus.com/reference/sstream/ostringstream/?kw=ostringstream 记得c++标准模板看过,当时觉得没啥用,还真有用

 

std::ostringstream的更多相关文章

  1. std::ostringstream输出流详解

    一.简单介绍 ostringstream是C++的一个字符集操作模板类,定义在sstream.h头文件中.ostringstream类通常用于执行C风格的串流的输出操作,格式化字符串,避免申请大量的缓 ...

  2. 【转载】C++中替代sprintf的std::ostringstream输出流详解

    一.简单介绍 ostringstream是C++的一个字符集操作模板类,定义在sstream.h头文件中.ostringstream类通常用于执行C风格的串流的输出操作,格式化字符串,避免申请大量的缓 ...

  3. std::ostringstream 转std::string

    http://www.cplusplus.com/reference/sstream/ostringstream/ https://en.cppreference.com/w/cpp/io/basic ...

  4. C++ 通过ostringstream 实现任意类型转string

    #include <iostream> #include <string> using namespace std; int main() { ; double b = 65. ...

  5. c++ istream转换为std::string

    std::istreambuf_iterator<char> eos; std::string s(std::istreambuf_iterator<char>(stream) ...

  6. C++ std::thread

    std::thread Defined in header class thread The class thread represents a single thread of execution. ...

  7. istringstream和ostringstream的实现

    ostringstream是将数据写入string里边的,istringstream是将从string里边读出数据的: #include <sstream> int main() { st ...

  8. 数值类型与std::string的相互转换

    1.使用std::stringstream: //将in_value值转换成out_type类型 template<class out_type, class in_value> out_ ...

  9. stringstream istringstream ostringstream 三者的区别

    stringstream istringstream ostringstream 三者的区别 说明 ostringstream : 用于执行C风格字符串的输出操作. istringstream : 用 ...

随机推荐

  1. Python 算法(1) 快速排序

    快速排序(quickSort) 快排的思想:首先任意选取一个数据(通常选用数组的第一个数)作为关键数据,然后将所有比它小的数都放到它前面,所有比它大的数都放到它后面,这个过程称为一趟快速排序. 百度百 ...

  2. Apache、Tomcat负载均衡与集群

    一. 环境准备 1.软件下载 a) apache_2.0.55-win32-x86-no_ssl.msi: b) apache-tomcat-5.5.17.rar c) mod_jk-apache-2 ...

  3. 关于TimeSpan

    一秒是1000万个tick TimeSpan ts = * ); Console.WriteLine(ts); Console.Read(); //print 00:00:01 并且在TimeSpan ...

  4. [转]如何为图片添加热点链接?(map + area)

    原文地址:https://www.cnblogs.com/jf-67/p/8135004.html 所谓图片热点链接就是为图片指定一个或多个区域以实现点击跳转到指定的页面.简单来说就是点击某一区域就能 ...

  5. 第7章 Iptables与Firewalld防火墙。

    第7章 Iptables与Firewalld防火墙.     Chapter7_听较强节奏的音乐能够让您更长时间的投入在学习中. <Linux就该这么学> 00:00/00:00     ...

  6. 在无法单步调试的情况下找Bug的技巧

    比如说你有一个大的模块A,其组成部分有B,C,D这3个小的模块,现在A出了一个BUG,因为某种原因的限制你无法单步调试.怎么较快地定位BUG发生的根源? 这里记录一下刚才我在找BUG的时候采用的思路, ...

  7. 解决VisualStudio2013无法查看数组内容的问题

    症状: 在使用VS2013调试的时候,数组只能查看第一个元素的值.如图 解决方案: 调试>窗口>内存 输入数组的内存地址,右击内存窗口>带符号显示(也可以选择16进制显示,看你自己的 ...

  8. jquery 修改 bootstrap模态框的宽度并且居中

    1.定义模态框 <div class="modal fade" id="Project_Cell_Modal" tabindex="-1&quo ...

  9. windows测试模式

    测试模式通常意义就是让windows 操作系统在测试状态下运行,windows操作系统在这种模式下可以运行非官方或无数字签名的驱动程序 .   目录 1 定义 2 进入/退出windows测试模式方法 ...

  10. swift侧开菜单

    此文来自学习这篇博客后的学习笔记,原博客是用oc写的,我最近在学swift,于是改写为swift. swift和oc之间互相调用还是很方便的,但是要注意AnyObject和optional的运用,我现 ...