GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::flush(3) C++ Standard Libary std::flush(3)

std::flush - std::flush


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.


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 or 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). 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.


os - reference to output stream


os (reference to the stream after manipulation)


Without std::flush, 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::milliseconds>(d).count()
<< " ms).." << 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';
}


..(450 ms)....(901 ms)....(1350 ms)....(1800 ms)....(2250 ms)..


unitbuf controls whether output is flushed after each operation
nounitbuf (function)
endl outputs '\n' and flushes the output stream
(function template)
flush synchronizes with the underlying storage device
(public member function of std::basic_ostream<CharT,Traits>)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.