std::unitbuf, std::nounitbuf
浮点格式化 | |||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
整数格式化 | |||||||||||||||||||||||||||||||
布尔格式化 | |||||||||||||||||||||||||||||||
域宽与填充控制 | |||||||||||||||||||||||||||||||
其他格式化 | |||||||||||||||||||||||||||||||
空白符处理 | |||||||||||||||||||||||||||||||
输出冲入 | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
状态标志操纵 | |||||||||||||||||||||||||||||||
时间与金钱 I/O | |||||||||||||||||||||||||||||||
带引号操纵符 | |||||||||||||||||||||||||||||||
(C++14)
|
在标头
<ios>
定义
|
||
std::ios_base& unitbuf( std::ios_base& str );
|
(1) | |
std::ios_base& nounitbuf( std::ios_base& str );
|
(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 或另一显式冲入,输出相同,但不实时出现。
#include <iostream> #include <chrono> template<typename Diff> void log_progress(Diff d) { std::cout << "..(" << std::chrono::duration_cast<std::chrono::milliseconds>(d).count() << " ms).."; } int main() { volatile int sink = 0; std::cout << std::unitbuf; // 启用自动冲入 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; // 做一些工作 auto now = std::chrono::high_resolution_clock::now(); log_progress(now - t1); } std::cout << '\n'; }
输出:
..(450 ms)....(902 ms)....(1352 ms)....(1802 ms)....(2252 ms)..
参阅
冲洗输出流 (函数模板) |
|
输出 '\n' 并冲洗输出流 (函数模板) |