简体   繁体   English

如何清除 std::ostream

[英]How to clear std::ostream

How do we clear or empty a std::ostream object?我们如何清除或清空 std::ostream 对象?

The examples I have found here are for stringstream;我在此处找到的示例适用于 stringstream;

stringstream m;
m.str("");

Obviously that does not work with ostream.显然,这不适用于 ostream。

EDIT: as asked, for usage:编辑:如所问,用法:

I use the ostream object in a simple Stopwatch.我在一个简单的秒表中使用 ostream 对象。 To trace the time of slow functions.跟踪慢函数的时间。 With the time the stream object is getting bigger and bigger, and the app goes unresponsive.随着时间流对象变得越来越大,应用程序变得无响应。 So here I am trying to clear the ostream object.所以在这里我试图清除 ostream 对象。 Preferable in the constructor of CStopUhr.首选在 CStopUhr 的构造函数中。

static tstringstream fout;                         // fout to stringstream
//static tofstream fout(_T("SpooferTimeTrace.log); // fout to file 
static std::wostream fout(std::wcout.rdbuf());     // fout to cout

class CStopUhr
{
public:
    CStopUhr(tostream& os, LPCTSTR txt = _T(" Time: ")) 
     :_os(os) 
      { _os.clear(); _txt = txt; _dwStart = GetTickCount(); }
    ~CStopUhr()         
      { _os << _txt << (GetTickCount()-_dwStart) << _T("ms ") << std::endl; }
private:
    DWORD _dwStart;
    tostream& _os;
    tstring  _txt;
};

usage:用法:

 void testfunction()
 {
    CStopUhr suhr(fout, CString(__FUNCTION__);
    :
    ;
}

我看不出你这样做的一个很好的理由,但也许你可以直接干扰底层的std::streambuf对象,以碰撞put指针直到缓冲区中没有更多突出的字符?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM