许多c++书籍包含这样的示例代码…

std::cout << "Test line" << std::endl;

...所以我也一直这么做。但我看到过很多开发者写的这样的代码:

std::cout << "Test line\n";

是否有技术上的原因,更喜欢其中一个,或者只是编码风格的问题?


当前回答

我记得在标准中读到过这个,所以是这样的:

参见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.

这意味着当且仅当std::cout和std::cin引用非交互设备时,它们将被完全缓冲。换句话说,如果stdout附加到终端,那么行为上没有区别。

然而,如果std::cout.sync_with_stdio(false)被调用,那么'\n'即使对交互设备也不会导致刷新。否则'\n'等同于std::endl,除非管道到std::endl上的files: c++ ref。

其他回答

这是一个仅输出的I/O操纵器。

std::endl将一个换行符插入到输出序列os中并将其刷新,就像调用os.put(os. width ('\n'))然后再调用os.flush()一样。

使用时间:

这个机械手可以用来立即产生一行输出,

如。

当显示来自长时间运行的进程、多个线程的日志活动或可能意外崩溃的程序的日志活动的输出时。

Also

如果派生进程执行任何屏幕I/O,则在调用std::system之前还需要显式刷新std::cout。在大多数其他常见的交互式I/O场景中,std::endl与std::cout一起使用时是冗余的,因为std::cin的任何输入、std::cerr的输出或程序终止都会强制调用std::cout.flush()。使用std::endl来代替'\n',这被一些来源所鼓励,可能会显著降低输出性能。

可能存在性能问题,std::endl强制刷新输出流。

它们都将写入适当的行尾字符。除此之外,endl将导致缓冲区被提交。在执行文件I/O时,通常不希望使用endl,因为不必要的提交会影响性能。

不同的行结束字符无关紧要,假设文件以文本模式打开,除非您要求二进制,否则您将得到文本模式。编译后的程序将为被编译的系统写出正确的东西。

唯一的区别是std::endl会刷新输出缓冲区,而'\n'不会。如果不希望频繁刷新缓冲区,请使用'\n'。如果你这样做(例如,如果你想获得所有的输出,而程序是不稳定的),使用std::endl。

我记得在标准中读到过这个,所以是这样的:

参见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.

这意味着当且仅当std::cout和std::cin引用非交互设备时,它们将被完全缓冲。换句话说,如果stdout附加到终端,那么行为上没有区别。

然而,如果std::cout.sync_with_stdio(false)被调用,那么'\n'即使对交互设备也不会导致刷新。否则'\n'等同于std::endl,除非管道到std::endl上的files: c++ ref。