您的位置:首页 > Web前端

Difference between \n and endl

2018-01-16 14:21 423 查看

Difference between \n and endl

C++ Primer Plus 6 Page 38

Typically, this book uses an embedded newline character (\n) when displaying quoted strings and endl manipulator otherwise.One difference is that endl guarantees the output will be flushed ( in, this case, immediately displayed onscreen )before the program moves on.You don’t get that guarantee with “\n”,which means that it is possible on some system in some circumstances a prompt might not be displayed until after you enter the information being prompted for.

The newline charcter is one example of special keystroke combinations termed “escape sequences”;they are further discussed in Chapter 3

Bing Translation

通常, 本书在显示带引号的字符串和 endl 操作符时使用嵌入的换行符 (\n)。一个区别是, endl 保证输出将被刷新 (在这种情况下, 在屏幕上立即显示), 然后程序继续前进。你没有得到 “\n” 的保证, 这意味着在某些情况下, 可能在输入提示信息之前, 可能不会显示提示。

换行性格是特殊按键组合的一个例子, 称为 “转义序列”; 它们在3章中进一步讨论

Chinese Book Translation

一个差别是,endl确保程序继续运行前刷新输出(将其立即显示在屏幕上);而使用“\n”不能提供这样的保证,这意味着在有些系统中,有时可能在您输入信息后才会出现提示

ostream Line 554

/**
*  @brief  Write a newline and flush the stream.
*
*  This manipulator is often mistakenly used when a simple newline is
*  desired, leading to poor buffering performance.  See
*  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html *  for more on this subject.
*/
template<typename _CharT, typename _Traits>
inline basic_ostream<_CharT, _Traits>&
endl(basic_ostream<_CharT, _Traits>& __os)
{ return flush(__os.put(__os.widen('\n'))); }


划重点:Write a newline and flush the stream.

写一个新行并且刷新流(缓冲区?)

Example

\n仅仅作为一个字符被写入流,没有任何特权。但是endl在输出一个’\n’后,还强制刷新了流,确保所有的信息都被写入。

另一个,在不同的系统下,换行符不一致,如果采用endl,可以增加移植性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: