如何清除std::ostream

How to clear std::ostream

本文关键字:std ostream 清除 何清除      更新时间:2023-10-16

如何清除或清空std::ostream对象?

我在这里找到的例子是字符串流;

stringstream m;
m.str("");

显然,这对ostream不起作用。

编辑:按要求,用法:

我在一个简单的秒表中使用ostream对象。追踪慢函数的时间。随着时间的推移,流对象越来越大,应用程序也没有响应。所以我在这里试图清除ostream对象。在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;
};

用法:

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

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