std::unitbuf, std::nounitbuf
来自cppreference.com
| 在标头 <ios> 定义
|
||
| |
(1) | |
| |
(2) | |
启用或禁用任何输出操作后的自动冲洗。在输入时无效果。
1) 如同用调用
str.setf(std::ios_base::unitbuf) 启用流 str 中的 unitbuf 标志。2) 如同用调用
str.unsetf(std::ios_base::unitbuf) 禁用流 str 中的 unitbuf 标志。这是一个 I/O 操纵符,可用如 out << std::unitbuf 的表达式对任何 std::basic_ostream 类型的 out 或用如 in >> std::unitbuf 的表达式对任何 std::basic_istream 类型的 in 调用。
注解
在 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 或另一显式冲洗时,输出相同,但不实时出现。
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)
<< " ... ";
}
int main()
{
volatile int sink = 0;
std::cout << std::unitbuf; // 启用自动冲入
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; // 做一些工作
log_progress(std::chrono::high_resolution_clock::now() - start);
}
std::cout << '\n';
}
输出:
571ms ... 1146ms ... 1722ms ... 2294ms ... 2865ms ...
参阅
| 冲洗输出流 (函数模板) | |
输出 '\n' 并冲洗输出流 (函数模板) |