Home c++ When to write STD :: ENDL And when ' N'?

When to write STD :: ENDL And when ‘\ N’?

Author

Date

Category

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.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions