TO:xiterator

peter_prince 2001-07-20 02:06:51
你在关于ostrstream 中说的清空os object 的方法行不通呀
(o.str(""); //两个"之间无空格,用于清空o缓冲。)
那我怎么清空他呢
...全文
38 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
skt985 2002-01-11
  • 打赏
  • 举报
回复
30784关注! :)
xiterator 2001-07-24
  • 打赏
  • 举报
回复
下面是一段代码用于临时缓存输出信息,以达到重用ss_Buf的目的,而不是在循环体内(stack)每次循环都定义一个stingstream实例(即执行stringstream的构造/析构工作),使效率高一些。
在代码中用//**标识了清空stringstream ss_Buf对象的句子.

#pragma warning (disable: 4786)

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
vector<string> vs_Name;
vs_Name.push_back("How");
vs_Name.push_back("are");
vs_Name.push_back("you");

vector<string>::const_iterator i=vs_Name.begin();
vector<string>::const_iterator e=vs_Name.end();
stringstream ss_Buf; //定义在while(i != e)效率高一些
int index=0;
while (i != e)
{
ss_Buf.str(""); //**empty the memory buf
ss_Buf << *i << "__" << index++ << endl; //format output info
cout << ss_Buf.str();
i++;
}
return 0;
}

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧