简体   繁体   English

“冲洗流”是什么意思?

[英]What is meant by 'flushing the stream'?

I've read that '\\n' is preferred over 'endl' for new line in c++ because 'endl' inserts new line and flushes the stream.我已经读过,对于 C++ 中的新行,'\\n' 比 'endl' 更受欢迎,因为 'endl' 插入新行并刷新流。 Please tell me what is flushing the stream ?请告诉我什么是冲洗流?

Flushing a stream ensures that all data that has been written to that stream is output, including clearing any that may have been buffered.刷新流可确保输出已写入该流的所有数据,包括清除可能已缓冲的任何数据。

Some streams are buffered to aid performance, eg a stream writing to disk may buffer until the content reaches a block size.一些流被缓冲以提高性能,例如写入磁盘的流可以缓冲直到内容达到块大小。

When you flush the stream you force contents of the output stream to the default output medium the OS uses.当您刷新流时,您将输出流的内容强制到操作系统使用的默认输出介质。 The term stream is an abstraction of a construct that allows you to send or receive an unknown number of bytes.术语流是一种构造的抽象,它允许您发送或接收未知数量的字节。 In certain points in a program, the output stream is automatically flushed, flushing is not always necessary.在程序中的某些点,输出流会自动刷新,但并不总是需要刷新。 To reduce overhead and improve performance, a stream buffers its contents and only periodically "flushes" it.为了减少开销并提高性能,流会缓冲其内容并仅定期“刷新”它。 Examples of streams are cin (std::cin) and cout (std::cout) which are the input and output streams.流的示例是 cin (std::cin) 和 cout (std::cout),它们是输入和输出流。 "std::cin" has a buffer to store input data whereas "std::cout's" buffer is used to store data that's going to be sent to screen. "std::cin" 有一个缓冲区来存储输入数据,而 "std::cout's" 缓冲区用于存储将要发送到屏幕的数据。

Flushing a stream is more likely as Flushing the toilet, where in toilet Flushing the toilet is cleared and in Stream(I/O Stream) Flushing the "Buffer-memory" is cleared.(where here Buffer-memory is a memory which is used to store the data temporarily from/for the I/O streams).冲洗流更可能是冲洗厕所,在厕所冲洗厕所被清除,在 Stream(I/O Stream) 冲洗“缓冲区内存”被清除。(这里的缓冲区内存是一个使用的内存临时存储来自/用于 I/O 流的数据)。

So, everytime "Flushing the Stream(means using of "endl" )" might become the bottleneck of the program, means your program always slows down at this point.因此,每次“刷新流(意味着使用“endl”)”都可能成为程序的瓶颈,这意味着您的程序此时总是会变慢。

for more on buffer visit : this有关缓冲区访问的更多信息:

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

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