本文介绍了更好的是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们会很难写coutn,这将基本上在输入结束处放置换行符号。在使用控制台(这是我现在可以做的所有)我发现非常乏味,每当我想要一行是一个新行写\\\


或者也许它是已经实现?

Guys would it be difficult to write coutn which would basically place newline symbol at the end of the input. While working with console (that's all I can do at the moment) I'm finding very tedious to write '\n' every time I want the line to be a new line.
Or maybe it is already implemented?

推荐答案

为避免单行上的多次注入,可以使用临时对象。这个临时对象将在其析构函数中添加'\\\
'。

To circumvent the multiple injections on a single line, you could use a temporary object. This temporary object would add the '\n' in its destructor.

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" << "!";

最后,我想知道这个 coutn 实际上是更好的?

In the end, I'm wondering if this coutn is actually better?

这篇关于更好的是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 19:52