[英]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.