浙江省高等学校教师教育理论培训

微信搜索“教师资格证岗前培训”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

stringstream clear()的疑问 - yuanshuilee的日志 - 网易博客

stringstream clear()的疑问  

2013-09-05 08:43:13|  分类: 基础代码(一天一 |  标签: |举报 |字号 订阅

 
 
很奇怪的一件事哦,我运行下面这条代码,看看程序使用内存情况,结果发现内存使用是在不断的增加。

#include<cstdlib>
#include<iostream>
#include<sstream>
usingnamespace std;
int main(int argc,char* argv[])
{
stringstream ss;
string str;
while(true)
{
ss.clear();//它并不清空任何内容,它只是重置了流的状态标志而已!
//ss.str("");
ss<<"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
ss>>str;
cout<<str<<endl;
// 去掉下面两行注释,看看每次循环,你的内存消耗增加了多少!
/* cout<<"Size of stream = "<<ss.str().length()<<endl;
system("PAUSE");*/
}
return0;
}

但是更奇怪的在后面。现在循环只有三行,但是内存的使用却不再增加了。

#include<cstdlib>
#include<iostream>
#include<sstream>
usingnamespace std;
int main(int argc,char* argv[])
{
stringstream ss;
string str;
while(true)
{
//ss.clear();//它并不清空任何内容,它只是重置了流的状态标志而已!
//ss.str("");
ss<<"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
ss>>str;
cout<<str<<endl;
// 去掉下面两行注释,看看每次循环,你的内存消耗增加了多少!
/* cout<<"Size of stream = "<<ss.str().length()<<endl;
system("PAUSE");*/
}
return0;
}

posted on 2014-01-16 18:04  lexus  阅读(311)  评论(0编辑  收藏  举报