What is meant by 'flushing the stream'?

后端 未结 2 1088
我寻月下人不归
我寻月下人不归 2021-02-01 23:17

I\'ve read that \'\\n\' is preferred over \'endl\' for new line in c++ because \'endl\' inserts new line and flushes the stream. Please tell me what is flushing the stream ?

2条回答
  •  盖世英雄少女心
    2021-02-02 00:13

    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. "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.

提交回复
热议问题