std::flush
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <ostream> 中定义
|
||
template< class CharT, class Traits > std::basic_ostream<charT,traits>& flush( std::basic_ostream<CharT, Traits>& os ); |
||
刷新的输出序列
os
为通过调用os.flush(). 原文:
Flushes the output sequence
os
as if by calling os.flush(). 这是一个只输出的I / O机械手,它可被称为用一个表达式如out << std::flush任何
out
类型std::basic_ostream原文:
This is an output-only I/O manipulator, it may be called with an expression such as out << std::flush for any
out
of type std::basic_ostream.目录 |
[编辑] 注释
产生立即输出一个不完整的行,例如,可以使用该机械手显示输出时,从一个长期运行的进程,多线程的活动记录或记录活动的程序,可能会意外崩溃。 std::cout还需要一个明确的冲洗调用std::system之前,如果产生的进程进行任何屏幕I / O(一个常见的例子是在Windowsstd::system("pause"))。在大多数其他常见的互动式I / O的情况std::endl是多余的,因为std::cout任何输入,输出到std::cin或程序终止,强制调用std::cerr使用时std::cout.flush().
原文:
This manipulator may be used to produce an incomplete line of output immediately, e.g. when displaying output from a long-running process, logging activity of multiple threads or logging activity of a program that may crash unexpectedly. An explicit flush of std::cout is also necessary before a call to std::system, if the spawned process performs any screen I/O (a common example is std::system("pause") on Windows). In most other usual interactive I/O scenarios, std::endl is redundant when used with std::cout because any input from std::cin, output to std::cerr, or program termination forces a call to std::cout.flush().
当一个完整的输出需要被刷新,std::endl机械手可能会被使用
原文:
When a complete line of output needs to be flushed, the std::endl manipulator may be used.
当每个输出操作都需要被刷新,std::unitbuf机械手可能会被使用
原文:
When every output operation needs to be flushed, the std::unitbuf manipulator may be used.
[编辑] 参数
os | - | 引用到输出流
|
[编辑] 返回值
os
(参考操作后到流)原文:
os
(reference to the stream after manipulation)[编辑] 示例
不使用的std ::冲洗,输出是相同的,但可能不会出现在实时.
原文:
Without std::flush, the output would be the same, but may 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).." << 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'; }
输出:
..(450 ms)....(901 ms)....(1350 ms)....(1800 ms)....(2250 ms)..
[编辑] 另请参阅
控制输出是否每次操作后冲洗 原文: controls whether output is flushed after each operation (函数) | |
输出'\n'并刷新输出流 原文: outputs '\n' and flushes the output stream (函数模板) | |
与底层存储设备同步 原文: synchronizes with the underlying storage device (公共成员函数of std::basic_ostream )
|