简体   繁体   English

C ++使用cout避免换行<

[英]C++ avoiding newline with cout <<

How could I avoid the newline in this code.. 如何避免在此代码中使用换行符。

void ListEl::display() {
    BaseEl::display();
    cout << " Asis: " << anemnesis << endl;
}

here is BaseEl::display() 这是BaseEl :: display()

void BaseEl::display() {
    cout << "P: " << priority << "\tN: " <<  name << endl;
}

it prints always the output of BaseEl::display(); 它总是打印BaseEl::display();的输出BaseEl::display(); then a newline, and then the " Asis: " << anemnesis << endl; 然后是换行符,然后是" Asis: " << anemnesis << endl;

I tried cout << BaseEl::display() << " Asis: " << anemnesis << endl; 我尝试了cout << BaseEl::display() << " Asis: " << anemnesis << endl; but it didnt work neither 但它也不起作用

You cannot fix this without modifying BaseEl::display() to stop producing new line at the end of the output. 您必须修改BaseEl::display()才能在输出末尾停止产生新行,才能解决此问题。

In general, it is a bad idea to add endl to your own output. 通常,将endl添加到您自己的输出中是个坏主意。 Let the caller do that if he needs a newline. 如果呼叫者需要换行符,则让呼叫者这样做。

Note that a more C++-like approach to output of your own classes is providing an implementation of operator << for the output. 请注意,一种更类似于C ++的方法来输出您自己的类是为输出提供operator <<的实现。 If you want virtual dispatch with it, provide an implementation at the level of the base class, and add a virtual member function for derived classes to override: 如果要使用它进行虚拟调度,请在基类级别提供一个实现,然后为派生类添加一个虚拟成员函数以覆盖:

class BaseEl {
protected:
    virtual void writeToStream(ostream& ostr) const;
    friend ostream& operator << (ostream& ostr, const BaseEl& val);
};
class ListEl : public BaseEl {
protected:
    virtual void writeToStream(ostream& ostr) const;
};

ostream& operator << (ostream& ostr, const BaseEl& val) {
    val.writeToStream(ostr);
    return ostr;
}

Answer 回答

The meaning of endl is to print a newline-character and flush the output buffer. endl的意思是打印换行符并刷新输出缓冲区。 Hence you need to remove it from where you do not want a newline character. 因此,您需要将其从不需要换行符的位置删除。

Flushing the output buffers excessively can lead to performance loss and is normally not needed to be done manually, so as a general rule, avoid endl (unless, as said, you explicitly want to newline and flush). 过度刷新输出缓冲区可能会导致性能损失,通常不需要手动执行,因此通常应避免使用endl (除非如此,除非您明确要换行并刷新)。

In short, as general rules: 简而言之,作为一般规则:

  • to flush : use std::flush 冲洗 :使用std::flush
  • to newline : use '\\n' or append it to your string: "foobar!\\n" 到换行符 :使用'\\n'或将其附加到字符串: "foobar!\\n"
  • both : use std::endl 两者 :使用std::endl

Advice 忠告

In C++, object serialization is done through overloading operator<< and operator>> , such that you can write 在C ++中,对象序列化是通过重载operator<<operator>> ,因此您可以编写

ListEl mylist;
std::cout << "The list: " << mylist << '\n';

Canonically, for output, it looks like this: 规范地说,对于输出,它看起来像这样:

class Foobar {
    friend std::ostream& operator<< (std::ostream& os, Foobar const &);    
};

// might go into implementation file
std::ostream& operator<< (std::ostream& os, Foobar const &foobar) {
    // print work
    ....

    // Do not forget to return the stream
    return os;
}

Letting that operator be a friend is a consequence of operator<<(std::ostream &, Foobar const&) not being inline -able within Foobar . 让该操作符成为friendoperator<<(std::ostream &, Foobar const&)Foobar不能inline结果。 If the print-function does not need access to private members, skip the friend . 如果打印功能不需要访问私人成员,请跳过friend

Remove the endl from BaseEl::display() or do as @dasblinkenlight said: BaseEl::display()删除endl或按照@dasblinkenlight的说明进行操作:

void BaseEl::display() 
{
    cout << "P: " << priority << "\tN: " <<  name;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM