std::flush(3)
πΈ std::flush β the C++ Standard Library's "send it NOW" button πΈ
Hey bestie, welcome to std::flush(3) from the C++ Standard Library. It's a tiny manipulator with major main-character energy: it makes your output actually show up. Let's get into it. π«Ά
π NAME π
std::flush β std::flush (yes, literally, that's the name, periodt)
β¨ Synopsis β¨
Defined in header <ostream>.
template< class CharT, class Traits >
std::basic_ostream<CharT, Traits>& flush( std::basic_ostream<CharT,
Traits>& os );
Flushes the output sequence os as if by calling os.flush().
This is an output-only I/O manipulator, it may be called with an expression such as out << std::flush for any out of type std::basic_ostream.
π§ Notes π§
This manipulator may be used to produce an incomplete line of output immediately, e.g. when:
- displaying output from a long-running process
- logging activity of multiple threads
- logging activity of a program that may crash unexpectedly
An explicit flush of std::cout is also necessary before a call to std::system, if the spawned process performs any screen I/O (a common example is std::system("pause") on Windows). Very "why is my prompt invisible" energy. π
In most other usual interactive I/O scenarios, 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().
When a complete line of output needs to be flushed, the std::endl manipulator may be used.
When every output operation 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 π»
Without std::flush, the output would be the same, but may not appear in real time.
// Run this code
#include <chrono>
#include <iostream>
template<typename Diff>
void log_progress(Diff d)
{
std::cout <<
std::chrono::duration_cast<std::chrono::milliseconds>(d) << " ... "
<< std::flush;
}
int main()
{
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);
}
std::cout << '\n';
}
Possible output:
567ms ... 1137ms ... 1707ms ... 2269ms ... 2842ms ...
π See also π
unitbufβ controls whether output is flushed after each operationnounitbufβ (function)endlβ outputs'\n'and flushes the output stream (function template)flushβ synchronizes with the underlying storage device (public member function ofstd::basic_ostream<CharT,Traits>)
source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::flush(3) β byeee bestie, go flush that output π β¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms