+ 2

cout<<endl vs cout<<" \n" in c++

In c++ which is best during coding?

23rd Oct 2018, 5:13 PM
Rohit Kumar
Rohit Kumar - avatar
2 Answers
+ 2
cout << endl : Inserts a new line and flushes the stream cout << "\n" : Only inserts a new line. cout << endl; can be said equivalent to cout << ‘\n’ << flush; cout << “\n” seems performance wise better than cout << endl; unless flushing of stream is required. what flushing does? (answer from web) It means forcing the contents of the output stream to the default output medium the OS uses (the console on Windows and the terminal on *nix). A stream is a buffer used to store I/O data. For instance, "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.
23rd Oct 2018, 5:19 PM
Megatron
Megatron - avatar
0
when you keep flushing frequently, it consumes significant amount of time. It's necessary to use '\n' in competitive coding.
22nd Jan 2019, 8:24 PM
LiquidX
LiquidX - avatar