std::endl
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <ostream> 中定义
|
||
template< class CharT, class Traits > std::basic_ostream<charT,traits>& endl( std::basic_ostream<CharT, Traits>& os ); |
||
向os
中插入一个换行符并进行flush操作 -- 通过调用os.put(os.widen('\n'))然后os.flush()来完成.
这是一个只输出的I / O机械手,它可被称为用一个表达式如out << std::endl任何
out
类型std::basic_ostream原文:
This is an output-only I/O manipulator, it may be called with an expression such as out << std::endl for any
out
of type std::basic_ostream.目录 |
[编辑] 注释
该机械手可用于立即产生一行输出,例如显示输出时,从一个长期运行的进程,多线程的活动记录或记录活动的程序,可能会意外崩溃。 std::cout还需要一个明确的冲洗调用std::system之前,如果产生的进程进行任何屏幕I / O(一个常见的例子是在Windowsstd::system("pause"))。在大多数其他常见的互动式I / O的情况,std::endlstd::coutstd::cin的输入,输出,因为任何std::cerr,或计划终止使用时,强制调用std::cout.flush()是多余的。 std::endl'\n',鼓励一些消息来源的使用,可显着降低输出性能.
原文:
This manipulator may be used to produce a 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(). Use of std::endl in place of '\n', encouraged by some sources, may significantly degrade output performance.
在许多实现中,标准输出是行缓冲,写'\n'导致冲洗,无论如何,除非std::cout.sync_with_stdio(false)被执行死刑。在这种情况下,不仅降低了不必要的
endl
文件的输出性能,而不是标准输出.原文:
In many implementations, standard output is line-buffered, and writing '\n' causes a flush anyway, unless std::cout.sync_with_stdio(false) was executed. In those situations, unnecessary
endl
only degrades the performance of file output, not standard output.本维基上的follow Bjarne Stroustrup的代码示例在冲洗的标准输出,只在必要的.
原文:
The code samples on this wiki follow Bjarne Stroustrup in flushing the standard output only where necessary.
当一行不完整的输出需要被刷新,std::flush机械手可能会被使用
原文:
When an incomplete line of output needs to be flushed, the std::flush manipulator may be used.
当每一个字符的输出需要被刷新,std::unitbuf机械手可能会被使用
原文:
When every character of output needs to be flushed, the std::unitbuf manipulator may be used.
[编辑] 参数
os | - | 引用到输出流
|
[编辑] 返回值
os
(参考操作后到流)原文:
os
(reference to the stream after manipulation)[编辑] 示例
使用\ n,而不是endl,输出是相同的,但可能不会出现在实时.
原文:
With \n instead of endl, 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 passed" << std::endl; } int main() { std::cout.sync_with_stdio(false); // on some platforms, stdout flushes on \n 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); } }
输出:
487 ms passed 974 ms passed 1470 ms passed 1965 ms passed 2455 ms passed
[编辑] 另请参阅
控制输出是否每次操作后冲洗 原文: controls whether output is flushed after each operation (函数) | |
刷新输出流 (函数模板) | |
与底层存储设备同步 原文: synchronizes with the underlying storage device (公共成员函数of std::basic_ostream )
|