Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::flush

      From cppreference.com
      <cpp‎ |io‎ |manip
       
       
       
      Input/output manipulators
      Floating-point formatting
      Integer formatting
      Boolean formatting
      Field width and fill control
      Other formatting
      Whitespace processing
      Output flushing
      flush
      (C++20)  

      Status flags manipulation
      Time and money I/O
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      Quoted manipulator
      (C++14)
       
      Defined in header<ostream>
      template<class CharT,class Traits>
      std::basic_ostream<CharT, Traits>& flush(std::basic_ostream<CharT, Traits>& os);

      Flushes the output sequenceos as if by callingos.flush().

      This is an output-only I/O manipulator, it may be called with an expression such asout<< std::flush for anyout of typestd::basic_ostream.

      Contents

      [edit]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 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 (a common example isstd::system("pause") on Windows). 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().

      When a complete line of output needs to be flushed, thestd::endl manipulator may be used.

      When every output operation needs to be flushed, thestd::unitbuf manipulator may be used.

      [edit]Parameters

      os - reference to output stream

      [edit]Return value

      os (reference to the stream after manipulation).

      [edit]Example

      Withoutstd::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(){volatileint 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 workauto now=std::chrono::high_resolution_clock::now();        log_progress(now- t1);}std::cout<<'\n';}

      Possible output:

      567ms ... 1137ms ... 1707ms ... 2269ms ... 2842ms ...

      [edit]See also

      controls whether output is flushed after each operation
      (function)[edit]
      outputs'\n' and flushes the output stream
      (function template)[edit]
      synchronizes with the underlying storage device
      (public member function ofstd::basic_ostream<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/manip/flush&oldid=159202"

      [8]ページ先頭

      ©2009-2025 Movatter.jp