+ 2
cout<<endl vs cout<<" \n" in c++
In c++ which is best during coding?
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.
0
when you keep flushing frequently, it consumes significant amount of time. It's necessary to use '\n' in competitive coding.