FreeBSD Manual Pages
std::endl(3) C++ Standard Libary std::endl(3) NAME std::endl - std::endl Synopsis Defined in header <ostream> template< class CharT, class Traits > std::basic_ostream<CharT, Traits>& endl( std::basic_ostream<CharT, Traits>& os ); Inserts a newline character into the output sequence os and flushes it as if by calling os.put(os.widen('\n')) followed by os.flush(). This is an output-only I/O manipulator, it may be called with an ex- pression such as out << std::endl for any out of type std::basic_ostream. Notes This manipulator may be used to produce a line of output immedi- ately, e.g. when displaying output from a long-running process, logging activity of multiple threads or logging activity of a program that may crash unexpectedly. An ex- plicit flush of std::cout is also necessary before a call to std::system, if the spawned process performs any screen I/O. In most other usual interactive I/O scenar- ios, std::endl is redundant when used with std::cout because any input from std::cin, output to std::cerr, or program termination forces a call to std::cout.flush(). Use of std::endl in place of '\n', encouraged by some sources, may signifi- cantly degrade output performance. In many implementations, standard output is line-buffered, and writ- ing '\n' causes a flush anyway, unless std::ios::sync_with_stdio(false) was executed. In those situations, unnecessary endl only degrades the performance of file output, not standard output. The code samples on this wiki follow Bjarne Stroustrup and The C++ Core Guidelines in flushing the standard output only where necessary. When an incomplete line of output needs to be flushed, the std::flush manipulator may be used. When every character of output needs to be flushed, the std::unitbuf manipulator may be used. Parameters os - reference to output stream Return value os (reference to the stream after manipulation) Example With \n instead of endl, the output would be the same, but may not appear in real time. // Run this code #include <iostream> #include <chrono> template<typename Diff> void log_progress(Diff d) { std::cout << std::chrono::duration_cast<std::chrono::millisec- onds>(d).count() << " ms passed" << std::endl; } int main() { std::cout.sync_with_stdio(false); // on some platforms, stdout flushes on \n volatile int sink = 0; auto t1 = std::chrono::high_resolution_clock::now(); for (int j=0; j<5; ++j) { for (int n=0; n<10000; ++n) for (int m=0; m<20000; ++m) sink += m*n; // do some work auto now = std::chrono::high_resolution_clock::now(); log_progress(now - t1); } } Possible output: 487 ms passed 974 ms passed 1470 ms passed 1965 ms passed 2455 ms passed See also unitbuf controls whether output is flushed after each operation nounitbuf (function) flush flushes the output stream (function template) flush synchronizes with the underlying storage device (public member function of std::basic_os- tream<CharT,Traits>) http://cppreference.com 2022.07.31 std::endl(3)
NAME | Synopsis | Notes | Parameters | Return value | Example | Possible output: | See also
Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::endl&sektion=3&manpath=FreeBSD+Ports+15.0>
