应该始终使用std :: endl吗? [英] Should std::endl always be used?

查看:117
本文介绍了应该始终使用std :: endl吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在本书 C ++ Primer (第5版)的帮助下,我正在从C过渡到C ++,作者在其中指出以下内容:

I am jumping from C into C++ with the help of the book C++ Primer (5th edition) where the author states the following:


程序员经常在调试过程中添加打印语句。这样的
语句应始终刷新流。否则,如果程序
崩溃,则输出可能留在缓冲区中,从而导致错误的
推断出程序崩溃的位置。

Programmers often add print statements during debugging. Such statements should always flush the stream. Otherwise, if the program crashes, output may be left in the buffer, leading to incorrect inferences about where the program crashed.

但在线发布则表示相反;有人说不断刷新缓冲区对程序不利,并导致性能问题。

But posts online suggest otherwise; some say constant flushing of the buffer is bad for the program and causes performance issues.

我的问题:


  1. 何时应使用 std :: endl

  2. 作者是错误的还是我误解了他所说的内容?

  3. 您能给出需要刷新输出流的任何现实情况吗?

  1. When should you use std::endl?
  2. Is the author wrong or did I misunderstand any part of what he stated?
  3. Can you give any real-world scenarios where flushing the output stream is necessary?

PS


  1. 刷新缓冲区是什么意思?


推荐答案

点4和点3


从点4开始,因为其他所有内容都与点4有关,而点3与它相关紧密相关。

Point 4 and Point 3

Starting with point 4 because everything else hinges on it and point 3 because it is tightly related.

刷新流时,将获取流存储的所有数据并将其写入流所表示的基础介质中。

When you flush the stream, you are taking all data stored by the stream and writing it to the underlying medium represented by the stream.

刷新后,它已经完成,提交并可以被外界观察(或多或少。支持流的操作系统和硬件也可能会延迟写入,但是您无能为力那)。您必须先将其刷新才能阅读。如果它从未被刷新过,那么您将无法读取它。

When it is flushed it is done, committed and ready to be observed by the outside world (More or less. The operating system and the hardware backing the stream may also delay the write, but there isn't much you can do about that). You can't read it until it has been flushed. If it's never flushed, you can't read it.

问题是您不想经常写IO,因为从CPU耗尽的所有东西都花费了很长时间与CPU内部的相比。有时会慢几万倍。在CPU内部,您具有千兆赫兹和并行总线,一次可移动32位或更多位数据。在外面,兆赫经常一次移动一点。

The thing is you do not want to write to IO very often because anything that goes out of the CPU takes an ungodly amount of time compared to that which stays inside the CPU. Tens of thousands of times slower sometimes. Inside the CPU you have gigahertz and parallel buses moving data 32 or more bits at a time. Outside you have megahertz often moving a single bit at a time.

以文件为例。不仅驱动器访问以CPU速度的一小部分运行,而且如果每个字节直接进入磁盘,那么对于每个字节,您可能都必须

Take a file as a classic example. Not only is drive access running at a fraction of a speed of the CPU, but if every byte goes directly to a disk, then for every byte you may have to


  1. 在磁盘上找到该字节的类似物。

  2. 将该字节周围的扇区加载到内存中。因此,您可能要移动数百或数千个字节,而不是移动一个字节。通常为512字节或4096字节。

  3. 将字节写到内存中的扇区中

  4. 将内存中的扇区写回到磁盘中

  1. find that byte's analogue on the disk.
  2. load the sector around that byte into memory. So instead of moving one byte, you may be moving a few hundred or thousand. Usually 512 bytes or 4096 bytes.
  3. write the byte into the sector in memory
  4. write the sector in memory back to the disk

野蛮。想象一下这样做几百或几千次来写一个字符串。但是,如果只在字符串太大而无法保存或完成时才写该字符串怎么办?如果您写扇区而不是字节怎么办?然后,您可以

Brutal. Imagine doing this a few hundred or a few thousand times to write a single string. But what if you only wrote the string when it got too big to hold or you were done? What if you wrote in sectors rather than bytes? Then you could


  1. 在磁盘上找到该字节的类似扇区。

  2. 将存储的扇区写入磁盘

一次操作可能潜在数千个字节。

One operation for potentially thousands of byte in one shot.

第2点回到第4点/第3点,您看不到未冲洗的内容。如果要在屏幕上看到特定的输出并且现在要查看,请刷新。如果您想在程序崩溃之前发出调试消息,并且可能在没有将最后几条绝对必要的消息显示到屏幕的情况下终止,请刷新。历史上到处都是程序员在错误的地方寻找错误,因为他们没有得到最后几条未出错的错误消息。

Point 2 goes back to point four/three's you can't read what you don't flush. If you want to see a particular output on the screen and you want to see it now, you flush. If you want to get a debug message out before the program crashes, and likely terminates without getting those last few absolutely essential messages onto the screen, you flush. History is full of programmers looking in the wrong place for a bug because they didn't get those last few unflushed error messages.

您正在以程序速度为代价,以确保相对的确定性

You are trading program speed for the relative certainty that you will see an important message when you need to see it.

Point 1调用返回点2。 std: :endl 既是行尾,又是刷新流的指令。仅当需要行尾和冲洗时才少量使用它。如果不需要冲洗,只需发送并结束一行:'\n'

Point 1 calls back to point 2. std::endl is both an end of line and an instruction to flush the stream. You use it sparingly and only when you need both an end of line and a flush. If you don't need a flush, just send and end of line: '\n'.

请接受Pete Becker的建议并在可能的情况下使用 std :: cerr 进行错误和调试。这就是它的用途。它依靠蛮力和无知运作。这是痛苦的。太慢了几乎总是可以。

Take Pete Becker's advice and use std::cerr for errors and debugging where possible. That's what it was built for. It operates on brute force and ignorance. It's painful. It's slow. And it almost always works.

这篇关于应该始终使用std :: endl吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆