| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Floating-point formatting | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
| Integer formatting | |||||||||||||||||||||||||||||||
| Boolean formatting | |||||||||||||||||||||||||||||||
| Field width and fill control | |||||||||||||||||||||||||||||||
| Other formatting | |||||||||||||||||||||||||||||||
| Whitespace processing | |||||||||||||||||||||||||||||||
| Output flushing | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
| Status flags manipulation | |||||||||||||||||||||||||||||||
| Time and money I/O | |||||||||||||||||||||||||||||||
| Quoted manipulator | |||||||||||||||||||||||||||||||
(C++14) | |||||||||||||||||||||||||||||||
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 sequenceos and flushes it as if by callingos.put(os.widen('\n')) followed byos.flush().
This is an output-only I/O manipulator, it may be called with an expression such asout<< std::endl for anyout of typestd::basic_ostream.
Contents |
This manipulator may be used to produce a line of output immediately, 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 explicit flush ofstd::cout is also necessary before a call tostd::system, if the spawned process performs any screen I/O. In most other usual interactive I/O scenarios,std::endl is redundant when used withstd::cout because any input fromstd::cin, output tostd::cerr, or program termination forces a call tostd::cout.flush(). Use ofstd::endl in place of'\n', encouraged by some sources, may significantly degrade output performance.
In many implementations, standard output is line-buffered, and writing'\n' causes a flush anyway, unlessstd::ios::sync_with_stdio(false) was executed. In those situations, unnecessaryendl only degrades the performance of file output, not standard output.
The code samples on this wikifollow Bjarne Stroustrup andThe C++ Core Guidelines in flushing the standard output only where necessary.
When an incomplete line of output needs to be flushed, thestd::flush manipulator may be used.
When every character of output needs to be flushed, thestd::unitbuf manipulator may be used.
| os | - | reference to output stream |
os (reference to the stream after manipulation).
With'\n' instead ofendl, the output would be the same, but may not appear in real time.
#include <chrono>#include <iostream> template<typename Diff>void log_progress(Diff d){std::cout<<std::chrono::duration_cast<std::chrono::milliseconds>(d)<<" passed"<< std::endl;} int main(){std::cout.sync_with_stdio(false);// on some platforms, stdout flushes on \n staticvolatileint sink{};constauto t1=std::chrono::high_resolution_clock::now();for(int i=0; i<5;++i){for(int j=0; j<10000;++j)for(int k=0; k<20000;++k) sink+= i* j* k;// do some work log_progress(std::chrono::high_resolution_clock::now()- t1);}}
Possible output:
566ms passed1133ms passed1699ms passed2262ms passed2829ms passed
| controls whether output is flushed after each operation (function)[edit] | |
| flushes the output stream (function template)[edit] | |
| synchronizes with the underlying storage device (public member function of std::basic_ostream<CharT,Traits>)[edit] |