endl 主要如何影响完全缓冲的流?

how endl mainly affects fully buffered streams?

https://www.cplusplus.com/doc/tutorial/basic_io/

在以下站点中,就在 cin 标题之前,声明

The endl manipulator produces a newline character, exactly as the insertion of '\n' does; but it also has an additional behavior: the stream's buffer (if any) is flushed, which means that the output is requested to be physically written to the device, if it wasn't already. This affects mainly fully buffered streams, and cout is (generally) not a fully buffered stream.

我的问题是,为什么endl主要影响全缓冲流,而cout怎么不是全缓冲流?

输出流主要使用三种缓冲策略:

  1. 无缓冲 - 每次写入流都会立即写入底层输出设备。
  2. 行缓冲 - 对流的写入存储在内存中,直到写入换行符或缓冲区已满,此时缓冲区被刷新到底层输出设备。
  3. 完全缓冲 - 对流的写入存储在内存中,直到流的内部缓冲区已满,此时缓冲区被刷新到底层输出设备。

why endl mainly affects the fully buffered streams

从上面的描述中可以看出这一点。如果流是无缓冲的,那么 std::endl 不会做任何额外的工作;没有要刷新的缓冲区。如果流是行缓冲的,那么写一个换行符无论如何都会刷新缓冲区,所以 std::endl 不会做任何额外的事情。 std::endl 仅对完全缓冲的流进行任何额外工作。


how cout is not a fully buffered stream?

C++ 语言没有指定用于 std::cout 的缓冲策略,但是当程序的标准输出流连接到终端时,大多数实现要么不使用缓冲,要么不使用行缓冲。如果 stdout 被重定向到其他东西,比如文件,许多实现将切换到对 std::cout.

使用完全缓冲的流