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,可以增加移植性。
相关文章推荐
- The fundamental differences between "GET" and "POST"
- The difference between Const and ReadOnly
- what is the difference between definition and declaration in c
- Difference between HashMap, LinkedHashMap and TreeMap
- What is the difference between objectForKey and valueForKey?
- What is the difference between a von Neumann architecture and a Harvard architecture?
- Difference Between ASP.NET Server Controls,HTML Server Controls and HTML Intrinsic Controls
- What is the difference between error active and error passive?
- Difference between BTree and B+Tree
- Difference between ScrollView and ListView
- What is the difference between ++i and i++
- the difference between complex and reduced instruction set computer
- The difference between film faced plywood and wood I beam
- What is the difference between call and apply?(关键词:call/apply/invoke)
- Differences between ArrayList and Vector
- SQL SERVER – Difference Between SQL Server Compact Edition (CE) and SQL Server Express Edition
- Difference between DB File Sequential and Scattered Reads
- what is the difference between processes and threads
- The difference between the private and public clouds
- android: what is the difference between focused, enabled, pressed, and selected states?