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. ocr 识别 github 源码

    参考 [1] https://github.com/eragonruan/text-detection-ctpn [2] https://github.com/senlinuc/caffe_ocr [ ...

  2. springboot web 服务器选择

    Spring Boot 揭秘与实战(五) 服务器篇 - 其他内嵌服务器 拓展链接 http://www.jianshu.com/p/9710585258fb 发表于 2017-01-03 | Spri ...

  3. Python 爬虫实例(6)—— 爬取蚂蚁免费代理

    数据库表sql语句: CREATE TABLE `free_ip` ( `free_ip_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', `ip` ...

  4. hibernate中继承映射保存

    1 简单继承映射,在子类上进行映射配置,可以将父类属性直接配置在子类映射文件中. 简单例子如下:teacher类继承自Person类. public class Person { private in ...

  5. Linux下的MySQL主主复制

    为什么,会有mysql的主主复制.因为在一些高可用的环境中,mysql的主从不能满足现实中的一些实际需求.比如,一些流量大的网站数据库访问有了瓶颈,需要负载均衡的时候就用两个或者多个的mysql服务器 ...

  6. hdu 2066 一个人的旅行(dijkstra)

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  7. 线程相关函数(6)-pthread_cond_wait(),pthread_cond_signal(), 条件变量

    pthread_cond_tpthread_cond_initpthread_cond_destroypthread_cond_waitpthread_cond_timedwaitpthread_co ...

  8. CXAnimation.h动画类

    /**************************************************************************** 使用一个CCAnimation对象可以CCS ...

  9. la4730(并查集+树状数组)

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=30& ...

  10. vim 笔记

    1.替换 :s/vivian/sky/ 替换当前行第一个 vivian 为 sky :s/vivian/sky/g 替换当前行所有 vivian 为 sky :%s/vivian/sky/(等同于 : ...