basic_ostream::flush

To flush the stream for output.

  template<class charT, class traits>
  basic_ostream<charT, traits> &
  flush(basic_ostream<charT,traits> (os);  
Remarks

The manipulator flush, takes no external arguments, but is placed in the stream. The manipulator flush will attempt to release an output buffer for immediate use without waiting for an external input.

A reference to ostream. The this pointer is returned.

Note in the Example of basic_ostream:: flush usage: comment out the flush and both lines will display simultaneously at the end of the program.

See Also

basic_ostream::flush

Listing: Example of basic_ostream:: flush usage:
#include <iostream>
#include <iomanip>
#include <ctime>

class stopwatch {
private:
   double begin, set, end;    
public:
   stopwatch();
   ~stopwatch();
   void start();
   void stop();
};

stopwatch::stopwatch() 
{ 
using namespace std;
   begin = (double) clock() / CLOCKS_PER_SEC; 
   end    = 0.0; 

   start();
   {                      
   begin = (double) clock() / CLOCKS_PER_SEC; 
   end    = 0.0; 
   start();
   cout << "begin time the timer: " << flush;
} 
}

stopwatch::~stopwatch() 
{
using namespace std;
   stop();    // set end
   cout << "\nThe Object lasted: ";
   cout << fixed << setprecision(2)
      << end - begin << " seconds \n";
}

// clock ticks divided by ticks per second
void stopwatch::start() 
{
   using namespace std;
   set = double(clock()/CLOCKS_PER_SEC);
}

void stopwatch::stop() 
{
using namespace std;
    end = double(clock()/CLOCKS_PER_SEC);
}

void time_delay(unsigned short t);

int main()
{
using namespace std;
   stopwatch watch; // create object and initialize 
   time_delay(5);      
   return 0; // destructor called at return
}

//time delay function
void time_delay(unsigned short t)
{
using namespace std;
   time_t tStart, tEnd;
   time(&tStart);
   while(tStart + t > time(&tEnd)){};
}

Results:

  begin time the timer:
  < short pause >
  The Object lasted: 3.78 seconds