stringstream构造函数会特别消耗内存,似乎不打算主动释放内存(或许是为了提高效率),如果你要在程序中使用同一个流反复读写大量数据,将会造成大量的内部消耗,因此建议:
    1:调用clear()清除当前错误控制状态,其原型为 void clear (iostate state=goodbit);
    2:调用str("")将缓冲区清零,清除脏数据

  在多次转换中重复使用同一个stringstream(而不是每次都创建一个新的对象)对象最大的好处在于效率。stringstream对象的构造和析构函数通常是非常耗费CPU时间的。

//验证字符串流不会主动释放内存
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
int main()
{
ostringstream foo("hello world");
cout<<foo.str()<<endl; //hello world
cout<<foo.str()<<endl; //hello world istringstream bar("hello world");
string word;
while(bar>>word)
cout<<word; //helloworld
cout<<endl;
cout<<bar.str()<<endl; //hello world stringstream ss;
string str;
while()
{
ss<<"abcdefg";
ss>>str;
cout<<"Size of stream = "<<ss.str().length()<<endl; //7\n14\n21\n28...
ss.clear(); //重复使用同一字符串流,注意clear()
system("pause");
}
system("pause");
return ;
}
//清除错误标识及清空string buffer
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
int main()
{
stringstream ss("hello world");
char ch;
while(ss.get(ch))
cout<<ch; //hello world
cout<<endl; ss.clear(); //清除ss.get()置位的failbit
ss.str(""); //清空string buffer
ss<<"hello c++";
cout<<ss.str()<<endl; system("pause");
return ;
}

最新文章

  1. Netty(四)分隔符与定长解码器的使用
  2. android 在线升级借助开源中国App源码
  3. c语言-交换两个整数
  4. JavaScript的一些基本语句代码如下!!!!
  5. Careercup - Google面试题 - 5377673471721472
  6. 【开源项目12】Retrofit – Java(Android) 的REST 接口封装类库
  7. ruby 疑难点之—— attr_accessor attr_reader attr_writer
  8. 【C语言】4-指针
  9. Java中1.0 / 0.0 会输出什么?
  10. PhP数据库 Mysql dos命令
  11. Python&#160;利用Python操作excel表格之xlwt介绍
  12. 无状态shiro认证组件(禁用默认session)
  13. base64的编码
  14. AMAZON数据集
  15. sql优化方法学习和总结
  16. ubuntu 14.04 安装Eclipse与配置环境变量
  17. shell (check return of each line)(PIPESTATUS[@])and sudoer
  18. webservice使用方法
  19. ZOJ 3686 A Simple Tree Problem(线段树)
  20. 【学习笔记】动态树Link-Cut-Tree

热门文章

  1. 原创: EasyUI Tree 最后一级 节点 横向排列
  2. leetcode 1
  3. if [-f build/core/envsetup.mk -a -f Makefile ]; then
  4. Android IOS WebRTC 音视频开发总结(四三)-- 诚信交易案例分享
  5. 信息图形(Infographic)
  6. 开源自己的一个小android项目(美女撕衣服游戏)
  7. mysql触发器关联表更新
  8. 使用CSS创建有图标的网站导航菜单
  9. Sublime Text 使用 Emmet 补全错误问题
  10. 复杂的databinding接受Ilist作为数据源