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 : 用 ...
随机推荐
- Web Socket rfc6455 握手 (C++)
std::string data((const char*)buf->data(),bytes_transferred); recycle_buffer(buf); std::string ke ...
- sqlzoo练习答案--SUM and COUNT
World Country Profile: Aggregate functions This tutorial is about aggregate functions such as COUNT, ...
- UVA 146 ID Codes(下一个排列)
C - ID Codes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Statu ...
- 每日英语:Yahoo's Rally: Made in China
The typical honeymoon doesn't last too long before the hard work of marriage begins. And so it norma ...
- iOS应用代码段瘦身办法
iOS应用代码段瘦身办法 大型app应对苹果官方代码段大小限制的小伎俩… 背景 苹果官方文档 对二进制 __TEXT 段大小有限制: 代码实在瘦不下去怎么办? 解决方案 利用 rename_secti ...
- [Linux]gcc/libc/glibc
转自:http://blog.csdn.net/weiwangchao_/article/details/16989713 1.gcc(gnu collect compiler)是一组编译工具的总称. ...
- asp.net 简单分页打印
<html> <head> <title>看看</title> <meta http-equiv="Content-Type" ...
- 后台登陆功能的实现 SESSION
控制器 <?php // 本类由系统自动生成,仅供测试用途 class IndexAction extends Action { public function index(){ $this-& ...
- 京东阅读PDF导出
适用平台:windows 需要软件:FastStone Capture(截图软件),TinyTask(操作录制软件) 1.打开京东阅读 2.设置截图软件 (1)设置截图区域(FastStone Cap ...
- 【cf489】D. Unbearable Controversy of Being(暴力)
http://codeforces.com/contest/489/problem/D 很显然,我们只需要找对于每个点能到达的深度为3的点的路径的数量,那么对于一个深度为3的点,如果有a种方式到达,那 ...