std::ostringstream
ostringstream是C++的一个字符集操作模板类,定义在sstream.h头文件中。ostringstream类通常用于执行C风格的串流的输出操作,格式化字符串,避免申请大量的缓冲区,替代sprintf。
派生关系图:
![]() |
![]() |
![]() |
ostringstream
|
ostringstream的构造函数形式:
- explicit ostringstream ( openmode which = ios_base::out );
- explicit ostringstream ( const string & str, openmode which = ios_base::out );
有时候,我们需要格式化一个字符串,但通常并不知道需要多大的缓冲区。为了保险常常申请大量的缓冲区以防止缓冲区过小造成字符串无法全部存储。这时我们可以考虑使用ostringstream类,该类能够根据内容自动分配内存,并且其对内存的管理也是相当的到位。
- #include <sstream>
- #include <string>
- #include <iostream>
- using namespace std;
- void main()
- {
- ostringstream ostr1; // 构造方式1
- ostringstream ostr2("abc"); // 构造方式2
- /*----------------------------------------------------------------------------
- *** 方法str()将缓冲区的内容复制到一个string对象中,并返回
- ----------------------------------------------------------------------------*/
- ostr1 << "ostr1" << 2012 << endl; // 格式化,此处endl也将格式化进ostr1中
- cout << ostr1.str();
- /*----------------------------------------------------------------------------
- *** 建议:在用put()方法时,先查看当前put pointer的值,防止误写
- ----------------------------------------------------------------------------*/
- long curPos = ostr2.tellp(); //返回当前插入的索引位置(即put pointer的值),从0开始
- cout << "curPos = " << curPos << endl;
- ostr2.seekp(2); // 手动设置put pointer的值
- ostr2.put('g'); // 在put pointer的位置上写入'g',并将put pointer指向下一个字符位置
- cout << ostr2.str() << endl;
- /*----------------------------------------------------------------------------
- *** 重复使用同一个ostringstream对象时,建议:
- *** 1:调用clear()清除当前错误控制状态,其原型为 void clear (iostate state=goodbit);
- *** 2:调用str("")将缓冲区清零,清除脏数据
- ----------------------------------------------------------------------------*/
- ostr2.clear();
- ostr2.str("");
- cout << ostr2.str() << endl;
- ostr2.str("_def");
- cout << ostr2.str() << endl;
- ostr2 << "gggghh"; // 覆盖原有的数据,并自动增加缓冲区
- cout << ostr2.str() << endl;
- }
详细用法请参考如下网址:http://www.cplusplus.com/reference/sstream/ostringstream/?kw=ostringstream 记得c++标准模板看过,当时觉得没啥用,还真有用
std::ostringstream的更多相关文章
- std::ostringstream输出流详解
一.简单介绍 ostringstream是C++的一个字符集操作模板类,定义在sstream.h头文件中.ostringstream类通常用于执行C风格的串流的输出操作,格式化字符串,避免申请大量的缓 ...
- 【转载】C++中替代sprintf的std::ostringstream输出流详解
一.简单介绍 ostringstream是C++的一个字符集操作模板类,定义在sstream.h头文件中.ostringstream类通常用于执行C风格的串流的输出操作,格式化字符串,避免申请大量的缓 ...
- std::ostringstream 转std::string
http://www.cplusplus.com/reference/sstream/ostringstream/ https://en.cppreference.com/w/cpp/io/basic ...
- C++ 通过ostringstream 实现任意类型转string
#include <iostream> #include <string> using namespace std; int main() { ; double b = 65. ...
- c++ istream转换为std::string
std::istreambuf_iterator<char> eos; std::string s(std::istreambuf_iterator<char>(stream) ...
- C++ std::thread
std::thread Defined in header class thread The class thread represents a single thread of execution. ...
- istringstream和ostringstream的实现
ostringstream是将数据写入string里边的,istringstream是将从string里边读出数据的: #include <sstream> int main() { st ...
- 数值类型与std::string的相互转换
1.使用std::stringstream: //将in_value值转换成out_type类型 template<class out_type, class in_value> out_ ...
- stringstream istringstream ostringstream 三者的区别
stringstream istringstream ostringstream 三者的区别 说明 ostringstream : 用于执行C风格字符串的输出操作. istringstream : 用 ...
随机推荐
- Android成长之路-实现简单动画
实现简单动画: 在drawable目录中放入图片, 并且创建xml文件 frame.xml 存入图片,如下: <pre class="html" name="cod ...
- HTTP协议-状态码详解(转)
原文:http://www.cnblogs.com/lebronjames/archive/2013/01/10/2854981.html HTTP状态码的学习资料到处都有,但是都是理论上讲解. 本 ...
- Mysql中的算术运算符详解
MySQL 支持的算术运算符包括加.减.乘.除和模运算.它们是最常使用.最简单的一类运算符.表4-1 列出了这些运算符及其作用. 下例中简单地描述了这几种运算符的使用方法: mysql> sel ...
- 419. Roman to Integer【medium】
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- Redis 面试题(持续更新)
前言 看了一圈,发现Redis的面试题主要问的是如下几块: 原理 用处(缓存/队列 包括Pub.Sub/计数器/排行榜等) 基本操作与数据类型 消息队列 且与其它消息队列的区别 主从备份 宕机如何处理 ...
- 用C语言(apue)实现 把时间戳转换为国标格式的字符串(2017-07-17 22:36:12)的函数
/*******************************************************************************/ /** *** 函 数 名: cha ...
- Logstash日志字段拆分grok
参考和测试网站:http://grokdebug.herokuapp.com 例如:test-39.dev.abc-inc.com Mon Apr 24 13:53:58 CST 2017 2017- ...
- Hadoop单机Hadoop测试环境搭建
Hadoop单机Hadoop测试环境搭建: 1. 安装jdk,并配置环境变量,配置ssh免密码登录 2. 下载安装包hadoop-2.7.3.tar.gz 3. 配置/etc/hosts 127.0. ...
- 好工具 VHD
通过powershell 互转 Convert-VHD –Path F:\debian.vhdx –DestinationPath F:\debian.vhd 举个栗子 附加参考 Convert-VH ...
- Shell脚本中调用另外一个脚本的方法
(转载): 在Linux平台上开发,经常会在console(控制台)上执行另外一个脚本文件,经常用的方法有:./my.sh 或 source my.sh 或 . my.sh:这三种方法有什么不同呢?我 ...
