簡體   English   中英

如何清除 std::ostream

[英]How to clear std::ostream

我們如何清除或清空 std::ostream 對象?

我在此處找到的示例適用於 stringstream;

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指針直到緩沖區中沒有更多突出的字符?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM