如何清空ostringstream中的内容

boris963 2003-10-14 09:31:32
一个ostringstream流对象先读入了一些字符,再次用时想清空原有的字符,该用什么函数呢?
...全文
455 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
boris963 2003-10-18
  • 打赏
  • 举报
回复
谢谢大家帮忙解决了问题!
Wolf0403 2003-10-16
  • 打赏
  • 举报
回复
干吗要清空它哩?超出 scope 直接释放了。或者干脆这样:重新构造一次好了^_^
Hot_Forever 2003-10-16
  • 打赏
  • 举报
回复
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
ostringstream os;
os<<"dec:"<<15<<hex<<"hex:"<<15<<endl;
cout<<os.str();
os.str("");
cout<<os.str()<<endl;
return 0;
}
os.str("")已经清空了ostringstream
nirvana_li 2003-10-16
  • 打赏
  • 举报
回复
回复人: boris963(天行健) ( ) 信誉:100 2003-10-15 10:46:06 得分:0



Hot_Forever(用钱砸死我吧)的方法是用来取得ostringstream中string的,但是不能把它清空.
liu_feng_fly(笑看风云 搏击苍穹 衔日月)说的ignore是istream中的,ostringstream继承自ostream,所以没有这个函数

楼主说的都没错,我试了试,的确。
关注。虚心学习。


fangrk 2003-10-15
  • 打赏
  • 举报
回复
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_ostringstream
: public basic_ostream<charT, traits> {
public:
typedef traits traits_type;
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
typedef basic_stringbuf<charT, traits, Allocator> sb_type;

typedef basic_ios<charT, traits> ios_type;
typedef basic_string<charT, traits, Allocator>
string_type;
explicit basic_ostringstream(ios_base::openmode which =
ios_base::out);
explicit basic_ostringstream(const string_type& str,
ios_base::openmode which =
ios_base::out);
virtual ~basic_ostringstream();
basic_stringbuf<charT,traits,Allocator> *rdbuf() const;

string_type str() const;
void str(const string_type& str);//就是这个啦!
};
fangrk 2003-10-15
  • 打赏
  • 举报
回复
::str("");
boris963 2003-10-15
  • 打赏
  • 举报
回复
Hot_Forever(用钱砸死我吧)的方法是用来取得ostringstream中string的,但是不能把它清空.
liu_feng_fly(笑看风云 搏击苍穹 衔日月)说的ignore是istream中的,ostringstream继承自ostream,所以没有这个函数

liu_feng_fly 2003-10-15
  • 打赏
  • 举报
回复
istream::ignore
istream& ignore( int nCount = 1, int delim = EOF );

Parameters

nCount

The maximum number of characters to extract.

delim

The delimiter character (defaults to EOF).

Remarks

Extracts and discards up to nCount characters. Extraction stops if the delimiter delim is extracted or the end of file is reached. If delim = EOF (the default), then only the end of file condition causes termination. The delimiter character is extracted.

basic_istream::ignore
basic_istream& ignore(streamsize n = 1,
int_type delim = T::eof());
The unformatted input function extracts up to n elements and discards them. If n equals numeric_limits<int>::max(), however, it is taken as arbitrarily large. Extraction stops early on end-of-file or on an element x such that T::to_int_type(x) compares equal to delim (which is also extracted). The function returns *this.

试以下这个
Hot_Forever 2003-10-15
  • 打赏
  • 举报
回复
用str函数:
ostringstream os;
os<<i<<j<<endl;
os.str();
Hot_Forever 2003-10-15
  • 打赏
  • 举报
回复
同意,os.str("")
(一)功能: 输出调试变量(类似于TRACE) (二)特点: 1.可以自动适应参数的类型(最主要依赖于ostringstream) 2.可以自动适应输入参数的个数。(关闭了编译提醒 #pragma warning(disable: 4003) ) 3.会在输出的变量值前面自动添加变量的名称,方便查看 4.程序创建Edit窗口用于输出转换后的字符串。 5.程序退出时会将输出字符串保存到工程目录下的DebugData.txt。方便查看 6.多种编译模式,比如可让DEBUG和Release版本都能输出调试变量或者两者都不输出 7.支持UNICODE,WIN32,Dll,MFC (三)使用说明: 1.把trace.h复制到工程目录下(可以不添加到工程)。 2.在文件"stdafx.h"里(文件的下方)添加 #include "trace.h"。之后就可以使用trace()宏了。 3.所有输出的字符串会保存在工程目录下的"DebugData.txt"文件,以方便查看 4.我把所有代码都放在一个头文件里,虽然不合符规范,但这样使用起来很方便。 5.trace(x,y,z,w)宏原本有4个参数,当参数不如时,编译器会给出警告,所以我使用 #pragma warning(disable: 4003) 把这个编译警告给关掉了。 (四)可以使用的宏: trace(X,Y,Z,W) //输出常用类型变量,如int、double、short、POINT、RECT、string //且自动适应变量的个数(变量数为1-4个) tracef() //格式化字符串,类似sprintf traceLastError()//输出系统错误代码,调用了GetLastError() traceRel(X,Y) //当X=true,输出"Y: successful" ; x=false,输出"Y: failed" traceClear() //清空窗口 (五)关于trace宏使能设置: 1.默认情况下 NO_TRACE_WINDOW 和 TRACE_WINDOW都没定义,则 DEBUG版本会输出调试字符串,而Release版本不会 2.如果开头定义了#define NO_TRACE_WINDOW DEBUG版本和Release版本都不会输出输出调试字符串 3.如果开头定义了#define TRACE_WINDOW DEBUG版本和Release版本都会输出输出调试字符串 4.每次修改上面2个宏后需要全部重新编译才会生效

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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