C++:“std::endl”与“\n”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/213907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: "std::endl" vs "\n"
提问by Head Geek
Many C++ books contain example code like this...
许多 C++ 书籍都包含这样的示例代码......
std::cout << "Test line" << std::endl;
...so I've always done that too. But I've seen a lot of code from working developers like this instead:
......所以我也一直这样做。但是我已经看到很多来自工作开发人员的代码是这样的:
std::cout << "Test line\n";
Is there a technical reason to prefer one over the other, or is it just a matter of coding style?
是否有技术上的原因选择一个而不是另一个,或者仅仅是编码风格的问题?
采纳答案by David Thornley
The varying line-ending characters don't matter, assuming the file is open in text mode, which is what you get unless you ask for binary. The compiled program will write out the correct thing for the system compiled for.
假设文件以文本模式打开,不同的行结束字符无关紧要,除非您要求二进制,否则您会得到这种结果。编译后的程序将为编译的系统写出正确的东西。
The only difference is that std::endl
flushes the output buffer, and '\n'
doesn't. If you don't want the buffer flushed frequently, use '\n'
. If you do (for example, if you want to get all the output, and the program is unstable), use std::endl
.
唯一的区别是std::endl
刷新输出缓冲区,而'\n'
不是。如果您不想频繁刷新缓冲区,请使用'\n'
. 如果您这样做(例如,如果您想获得所有输出,而程序不稳定),请使用std::endl
.
回答by Martin York
The difference can be illustrated by the following:
可以通过以下方式说明差异:
std::cout << std::endl;
is equivalent to
相当于
std::cout << '\n' << std::flush;
So,
所以,
- Use
std::endl
If you want to force an immediate flush to the output. - Use
\n
if you are worried about performance (which is probably not the case if you are using the<<
operator).
- 使用
std::endl
如果要强制立即刷新到输出。 \n
如果您担心性能,请使用(如果您使用<<
运算符,情况可能并非如此)。
I use \n
on most lines.
Then use std::endl
at the end of a paragraph (but that is just a habit and not usually necessary).
我\n
在大多数线路上使用。
然后std::endl
在段落末尾使用(但这只是一种习惯,通常不是必需的)。
Contrary to other claims, the \n
character is mapped to the correct platform end of line sequence only if the stream is going to a file (std::cin
and std::cout
being special but still files (or file-like)).
与其他声明相反,\n
只有当流将流向文件(std::cin
并且std::cout
是特殊但仍然是文件(或类似文件))时,字符才被映射到正确的平台行尾。
回答by Martin Beckett
There might be performance issues, std::endl
forces a flush of the output stream.
可能存在性能问题,std::endl
强制刷新输出流。
回答by Nathan
There's another function call implied in there if you're going to use std::endl
如果您要使用,其中还隐含了另一个函数调用 std::endl
a) std::cout << "Hello\n";
b) std::cout << "Hello" << std::endl;
a) calls operator <<
once.
b) calls operator <<
twice.
a) 呼叫操作员<<
一次。
b) 呼叫接线员<<
两次。
回答by Emily L.
I recalled reading about this in the standard, so here goes:
我记得在标准中读到过这个,所以这里是:
See C11 standard which defines how the standard streams behave, as C++ programs interface the CRT, the C11 standard should govern the flushing policy here.
请参阅 C11 标准,该标准定义了标准流的行为方式,因为 C++ 程序与 CRT 接口,因此 C11 标准应管理此处的刷新策略。
ISO/IEC 9899:201x
7.21.3 §7
At program startup, three text streams are predefined and need not be opened explicitly — standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.
7.21.3 §3
When a stream is unbuffered, characters are intended to appear from the source or at the destination as soon as possible. Otherwise characters may be accumulated and transmitted to or from the host environment as a block. When a stream is fully buffered, characters are intended to be transmitted to or from the host environment as a block when a buffer is filled. When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered. Furthermore, characters are intended to be transmitted as a block to the host environment when a buffer is filled, when input is requested on an unbuffered stream, or when input is requested on a line buffered stream that requires the transmission of characters from the host environment. Support for these characteristics is implementation-defined, and may be affected via the setbuf and setvbuf functions.
ISO/IEC 9899:201x
7.21.3 §7
在程序启动时,三个文本流是预定义的,不需要显式打开——标准输入(用于读取常规输入)、标准输出(用于写入常规输出)和标准错误(用于写入诊断输出)。最初打开时,标准错误流没有完全缓冲;当且仅当可以确定流不是指交互设备时,标准输入和标准输出流才被完全缓冲。
7.21.3 §3
当流没有缓冲时,字符会尽快从源或目标出现。否则,字符可能会作为一个块累积并传输到主机环境或从主机环境传输。当一个流被完全缓冲时,当缓冲区被填充时,字符将作为一个块传输到主机环境或从主机环境传输。当流被行缓冲时,当遇到换行符时,字符将作为块传输到主机环境或从主机环境传输。此外,当缓冲区被填满时,当在非缓冲流上请求输入时,或者当在需要从主机环境传输字符的行缓冲流上请求输入时,字符将作为一个块传输到主机环境.
This means that std::cout
and std::cin
are fully buffered if and only ifthey are referring to a non-interactive device. In other words, if stdout is attached to a terminal then there is no difference in behavior.
这意味着,std::cout
和std::cin
是完全缓冲当且仅当他们所指的非交互设备。换句话说,如果 stdout 附加到终端,则行为没有区别。
However, if std::cout.sync_with_stdio(false)
is called, then '\n'
will not cause a flush even to interactive devices. Otherwise '\n'
is equivalent to std::endl
unless piping to files: c++ ref on std::endl.
但是,如果std::cout.sync_with_stdio(false)
被调用,则'\n'
即使对交互式设备也不会导致刷新。否则'\n'
等效于std::endl
除非管道到文件:c++ ref on std::endl。
回答by Ferruccio
They will both write the appropriate end-of-line character(s). In addition to that endl will cause the buffer to be committed. You usually don't want to use endl when doing file I/O because the unnecessary commits can impact performance.
他们都会写出适当的行尾字符。除此之外, endl 将导致缓冲区被提交。您通常不想在执行文件 I/O 时使用 endl,因为不必要的提交会影响性能。
回答by ?zgür
Not a big deal, but endl won't workin boost::lambda.
没什么大不了的,但endl在boost::lambda 中不起作用。
(cout<<_1<<endl)(3); //error
(cout<<_1<<"\n")(3); //OK , prints 3
回答by smerlin
If you use Qt and endl
, you could accidentally end up using an incorrect endl
which gives you very surprising results. See the following code snippet:
如果您使用 Qt 和endl
,您可能会意外地使用不正确的endl
结果,这会给您带来非常令人惊讶的结果。请参阅以下代码片段:
#include <iostream>
#include <QtCore/QtCore>
#include <QtGui/QtGui>
// notice that there is no "using namespace std;"
int main(int argc, char** argv)
{
QApplication qapp(argc,argv);
QMainWindow mw;
mw.show();
std::cout << "Finished Execution!" << endl;
// This prints something similar to: "Finished Execution!67006AB4"
return qapp.exec();
}
Note that I wrote endl
instead of std::endl
(which would have been correct) and apparently there is a endl
function defined in qtextstream.h(which is part of QtCore).
请注意,我写了endl
而不是std::endl
(这本来是正确的)并且显然endl
在qtextstream.h(它是 QtCore 的一部分)中定义了一个函数。
Using "\n"
instead of endl
completely sidesteps any potential namespace issues.
This is also a good example why putting symbols into the global namespace (like Qt does by default) is a bad idea.
使用"\n"
而不是endl
完全回避任何潜在的命名空间问题。这也是一个很好的例子,为什么将符号放入全局命名空间(就像 Qt 默认情况下所做的那样)是一个坏主意。
回答by Zee JollyRoger
I've always had a habit of just using std::endl because it is easy for me to see.
我一直有只使用 std::endl 的习惯,因为它很容易让我看到。
回答by Zee JollyRoger
The std::endl
manipulator is equivalent to '\n'
. But std::endl
always flushes the stream.
该std::endl
机械手相当于 '\n'
。但std::endl
总是冲洗流。
std::cout << "Test line" << std::endl; // with flush
std::cout << "Test line\n"; // no flush