C++ 刷新缓冲区是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15042849/
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
What does flushing the buffer mean?
提问by Mohamed Ahmed Nabil
I am learning C++ and I found something that I can't understand:
我正在学习 C++,我发现了一些我无法理解的东西:
Output buffers can be explicitly flushed to force the buffer to be written. By default, reading
cin
flushescout
;cout
is also flushed when the program ends normally.
可以显式刷新输出缓冲区以强制写入缓冲区。默认情况下,读取
cin
刷新cout
;cout
当程序正常结束时也会被刷新。
So flushing the buffer (for example an output buffer): does this clear the buffer by deleting everything in it or does it clear the buffer by outputting everything in it? Or does flushing the buffer mean something completely different?
所以刷新缓冲区(例如输出缓冲区):这是通过删除缓冲区中的所有内容来清除缓冲区还是通过输出缓冲区中的所有内容来清除缓冲区?还是刷新缓冲区意味着完全不同的东西?
回答by David Heffernan
Consider writing to a file. This is an expensive operation. If in your code you write one byte at a time, then each write of a byte is going to be very costly. So a common way to improve performance is to store the data that you are writing in a temporary buffer. Only when there is a lot of data is the buffer written to the file. By postponing the writes, and writing a large block in one go, performance is improved.
考虑写入文件。这是一项昂贵的操作。如果在您的代码中一次写入一个字节,那么每次写入一个字节的成本将非常高。因此,提高性能的常用方法是将您正在写入的数据存储在临时缓冲区中。只有当数据很多时才将缓冲区写入文件。通过推迟写入,并一次性写入一个大块,性能得到了提高。
With this in mind, flushing the buffer is the act of transferring the data from the buffer to the file.
考虑到这一点,刷新缓冲区是将数据从缓冲区传输到文件的行为。
Does this clear the buffer by deleting everything in it or does it clear the buffer by outputting everything in it?
这是通过删除其中的所有内容来清除缓冲区还是通过输出其中的所有内容来清除缓冲区?
The latter.
后者。
回答by tc.
You've quoted the answer:
你引用了答案:
Output buffers can be explicitly flushed to force the buffer to be written.
可以显式刷新输出缓冲区以强制写入缓冲区。
That is, you may need to "flush" the output to cause it to be written to the underlying stream (which may be a file, or in the examples listed, a terminal).
也就是说,您可能需要“刷新”输出以使其写入底层流(它可能是一个文件,或者在列出的示例中是一个终端)。
Generally, stdout/cout is line-buffered: the output doesn't get sent to the OS until you write a newline or explicitly flush the buffer. The advantage is that something like std::cout << "Mouse moved (" << p.x << ", " << p.y << ")" << endl
causes only one write to the underlying "file" instead of six, which is much better for performance. The disadvantage is that a code like:
通常,stdout/cout 是行缓冲的:在您编写换行符或显式刷新缓冲区之前,输出不会发送到操作系统。优点是类似的东西std::cout << "Mouse moved (" << p.x << ", " << p.y << ")" << endl
只会导致一次写入底层“文件”而不是六次,这对性能来说要好得多。缺点是代码如下:
for (int i = 0; i < 5; i++) {
std::cout << ".";
sleep(1); // or something similar
}
std::cout << "\n";
will output .....
at once (for exact sleep
implementation, see this question). In such cases, you will want an additional << std::flush
to ensure that the output gets displayed.
将立即输出.....
(有关确切sleep
实现,请参阅此问题)。在这种情况下,您需要额外的内容<< std::flush
来确保显示输出。
Reading cin
flushes cout
so you don't need an explicit flush to do this:
读取cin
刷新,cout
因此您不需要显式刷新来执行此操作:
std::string colour;
std::cout << "Enter your favourite colour: ";
std::cin >> colour;
回答by Alex Chamberlain
Clear the buffer by outputting everything.
通过输出所有内容清除缓冲区。