C++核心准则​SL.io.50:避免使用endl

2020-10-30 11:34:58 浏览数 (1)

SL.io.50: Avoid endl

SL.io.50:避免使用endl

Reason(原因)

The endl manipulator is mostly equivalent to 'n' and "n"; as most commonly used it simply slows down output by doing redundant flush()s. This slowdown can be significant compared to printf-style output.

endl操纵符差不多等于是' n'和“ n”;使用它最常见的结果是通过执行多余的flush()来减慢输出速度。与printf样式的输出相比,这种速度下降可能会显著。

Example(示例)

代码语言:javascript复制
cout << "Hello, World!" << endl;    // two output operations and a flush
cout << "Hello, World!n";          // one output operation and no flush
Note(注意)

For cin/cout (and equivalent) interaction, there is no reason to flush; that's done automatically. For writing to a file, there is rarely a need to flush.

对于cin / cout(和等效的)交互,没有理由刷新;那是自动完成的。对于文件写入,几乎不需要刷新。

Note(注意)

Apart from the (occasionally important) issue of performance, the choice between 'n' and endl is almost completely aesthetic.

除了(有时很重要的)性能问题之外,在' n'和endl之间进行选择几乎是完全取决于美感的。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#slio50-avoid-endl

0 人点赞