伙计们很难写出coutn,它基本上会在输入的末尾放置换行符号.在使用控制台的时候(这就是我现在所能做的一切),每次我希望线路成为新线路时,我发现写'\n'非常繁琐.
或许它已经实施了?
Did*_*set 11
要绕过单行上的多次进样,可以使用临时对象.这个临时对象会在其析构函数中添加'\n'.
struct coutn {
coutn(): os(cout) {}
~coutn() { os << '\n'; }
template <typename T>
coutn & operator<<(T const & x) { os << x; return *this; }
private:
ostream &os;
};
coutn() << "Hello " << "World" << "!";
Run Code Online (Sandbox Code Playgroud)
最后,我想知道这coutn实际上是否更好?