关于流和东西的问题,我有多个问题,但经过一番思考,我得出的结论是,我所需要的只是一个自定义刷新类型。我希望我的流在换行时刷新。它省去了输入 std::endl 的麻烦。有可能实现这一点吗?我正在使用带有自定义 stringbuf 的 ostream。
问问题
388 次
1 回答
1
我相信它所需要的只是压倒一切ostream::put(char)
,但不要引用我的话:
template <typename Ch>
class autoflush_ostream : public basic_ostream<Ch> {
public:
typedef basic_ostream<Ch> Base;
autoflush_ostream& put(Ch c);
};
template <typename Ch>
autoflush_ostream<Ch>& autoflush_ostream<Ch>::put(Ch c) {
Base::put(c);
if (c == "\n") {
flush();
}
return *this;
}
您可能必须重写每个采用 STL 中定义的字符或字符序列的方法和函数。它们基本上都会做同样的事情:调用超类上定义的方法/函数,检查是否刚刚打印了换行符,如果是则刷新。
于 2010-12-11T17:04:15.447 回答