There are two ways to record a row translation – Std :: Endl
and \ N
. What is the difference? When what to use?
Answer 1, Authority 100%
std :: endl
implemented as os.put (os.widen ('\ n')); OS.Flush ();
.
Accordingly, all the difference is that Endl
calls flush ()
, and \ n
– no.
Call Flush ()
Clears the flow buffer, and if it is too often to perform it, it can significantly slow down the execution of the program.
also need to remember that std :: endl
is not needed where the standard library itself calls cout.flush ()
:
- when the program is completed
- when reading from
STD :: CIN
- When writing in
Std :: Cerr
Some implementations buffered output to stdout line, and the \ n
record will call Flush
.
Write a buffer can be clearly, by the STD :: Flush
, for example
std :: cout & lt; & lt; "Calculating xyz ..." & lt; & lt; STD :: Flush; wait_xyz (); STD :: COUT & LT; & LT; "Ok \ n";
Links
Answer 2
It is necessary to remember that in C++ files themselves do not close when leaving the program. It can be closed, but only in memory, and the file will not be visible on the disk. The previous man said correctly: depending on which goal is pursuing. It may not be necessary to flush so often, especially in the cycle.
Write “@”: to come to the mail. I accidentally saw your comment. In fact: there are no streams itself does not close (at least on the disk). Available in the object destructor, specify close ()
, so that the file is on the disk. In Pascal, it’s easier: there you can not specify closure, when leaving the program, it will close itself.