首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::flush

Defined in header <ostream>

?

?

template< class CharT, class Traits > std::basic_ostream<CharT, Traits>& flush( std::basic_ostream<CharT, Traits>& os );

?

?

刷新输出序列os好像通过打电话os.flush()...

这是一个只有输出的I/O操作程序,可以用如下表达式调用它out << std::flush对任何out类型std::basic_ostream...

注记

该机械手可用于立即产生不完整的输出行,例如,当显示来自长期运行的进程的输出、多线程的日志活动或可能意外崩溃的程序日志活动时。一次明显的冲过...std::cout在调用std::system,如果派生进程执行任何屏幕I/O%28,常见的示例是std::system("pause")在Windows%29上。在大多数其他通常的交互I/O场景中,std::endl在使用时是多余的。std::cout因为来自std::cin,输出到std::cerr,或者程序终止会强制调用std::cout.flush()...

当需要刷新完整的输出行时,std::endl可以使用机械手。

当每个输出操作都需要刷新时,std::unitbuf可以使用机械手。

参数

os

-

reference to output stream

返回值

os%28操作后对流的引用%29。

如果没有std::刷新,输出将是相同的,但可能不会实时出现。

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
..(450 ms)....(901 ms)....(1350 ms)....(1800 ms)....(2250 ms)..

二次

另见

unitbufnounitbuf

controls whether output is flushed after each operation (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)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com