c++endl

Why some examples are using "endl" instead of "\n"?


Why some examples are using "endl" instead of "\n"?

I heard that processing time of "endl" is longer than "\n" because "endl" has some flushing process.

But, there are so many examples using "endl" on the internet.

I wanna know why they're using "endl" which thought to be inefficient.

Sorry for the awkward English...... Thanks


Solution

  • Oftentimes, direct usage of std::cout is something not meant for production code, where it's usually preferred to use some kind of logging facility (for writing to a file instead of standard stream etc.). Within such logging library, you would want to take care you only flush when desired, but for some ad hoc, debugging-like output of stuff, the performance implications of std::endl don't really matter and it's more important to immediately see the desired output.