Namespaces
Variants

std:: unitbuf, std:: nounitbuf

From cppreference.net
< cpp ‎ | io ‎ | manip
Input/output manipulators
Floating-point formatting
Integer formatting
Boolean formatting
Field width and fill control
Other formatting
Whitespace processing
Output flushing
unitbuf nounitbuf
Status flags manipulation
Time and money I/O
(C++11)
(C++11)
(C++11)
(C++11)
Quoted manipulator
(C++14)
定义于头文件 <ios>
(1)
std:: ios_base & nounitbuf ( std:: ios_base & str ) ;
(2)

启用或禁用任何输出操作后输出流的自动刷新功能。对输入操作没有影响。

1) 启用流 str 中的 unitbuf 标志,如同通过调用 str. setf ( std:: ios_base :: unitbuf ) 实现。
2) 禁用流 str 中的 unitbuf 标志,如同通过调用 str. unsetf ( std:: ios_base :: unitbuf ) 实现。

这是一个I/O操纵器,可以通过形如 out << std :: unitbuf 的表达式调用(适用于任何 out 类型为 std::basic_ostream 的对象),或通过形如 in >> std :: unitbuf 的表达式调用(适用于任何 in 类型为 std::basic_istream 的对象)。

目录

注释

刷新操作在 std::basic_ostream::sentry 对象的析构函数中执行,当 str. flags ( ) & std:: ios_base :: unitbuf true 时,会调用 str. rdbuf ( ) - > pubsync ( )

标准输出对象 std::cerr std::wcerr 默认设置了 unitbuf 位。

参数

str - I/O 流的引用

返回值

str (指向经过操作后的流的引用)。

示例

在不使用 std::unitbuf 或其他显式刷新的情况下,输出结果相同,但不会实时显示。

#include <chrono>
#include <iostream>
template<typename Diff>
void log_progress(Diff d)
{
    std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(d)
              << " ... ";
}
int main()
{
    volatile int sink = 0;
    std::cout << std::unitbuf; // enable automatic flushing
    const auto start = 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
        log_progress(std::chrono::high_resolution_clock::now() - start);
    }
    std::cout << '\n';
}

输出:

571ms ... 1146ms ... 1722ms ... 2294ms ... 2865ms ...

参见

刷新输出流
(函数模板)
输出 ' \n ' 并刷新输出流
(函数模板)