std::unitbuf, std::nounitbuf
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <ios> 中定义
|
||
std::ios_base& unitbuf( std::ios_base& str ); |
(1) | |
std::ios_base& nounitbuf( std::ios_base& str ); |
(2) | |
启用或禁用任何输出操作后,自动冲洗的输出流。有没有对输入的影响.
1)
2)
原文:
Enables or disables automatic flushing of the output stream after any output operation. Has no effect on input.
这是一个I / O操纵器,它可被称为与表达式如out << std::unitbuf类型
out
任何std::basic_ostream,或与一个表达式如in >> std::unitbuf类型in
任何std::basic_istream.原文:
This is an I/O manipulator, it may be called with an expression such as out << std::unitbuf for any
out
of type std::basic_ostream or with an expression such as in >> std::unitbuf for any in
of type std::basic_istream.目录 |
[编辑] 注释
法拉盛std::basic_ostream::sentry对象的析构函数中进行,这就要求str.rdbuf()->pubsync()如果str.flags() & std::ios_base::unitbuf == true.
原文:
Flushing is performed in the destructor of the std::basic_ostream::sentry object, which calls str.rdbuf()->pubsync() if str.flags() & std::ios_base::unitbuf == true.
标准输出的对象std::cerr和std::wcerr
unitbuf
位设置在默认情况下.原文:
The standard output objects std::cerr and std::wcerr have their
unitbuf
bit set by default.[编辑] 参数
str | - | 参考I / O流
|
[编辑] 返回值
str
(参考操作后到流)原文:
str
(reference to the stream after manipulation)[编辑] 示例
不带std :: unitbuf的或其他明确冲洗的,输出是相同的,但不会出现在实时.
原文:
Without std::unitbuf or another explicit flush, the output is the same, but does not appear in real time.
#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; // enable automatic flushing 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'; }
输出:
..(450 ms)....(902 ms)....(1352 ms)....(1802 ms)....(2252 ms)..
[编辑] 另请参阅
刷新输出流 (函数模板) | |
输出'\n'并刷新输出流 原文: outputs '\n' and flushes the output stream (函数模板) |