C++ `endl` 与 `\n` 的区别

  • std::cout << std::endl : 插入换行并刷新缓存区 (flush the buffer)
  • std::cout << "\n" : 插入换行

其中关于 std::endl,C++ Primer 中是这样介绍的:

endl, which is a special value called a manipulator. Writing endl has the effect of ending the current line and flushing the buffer associated with that device. Flushing the buffer ensures that all the output the program has generated so far is actually written to the output stream, rather than sitting in memory waiting to be written.”

-- Stanley B. Lippman. “C++ Primer, Fifth Edition (Jason Arnold's Library).”

刷新操作能够保证程序中推送到 cout 的数据全部输出,而不是继续留存在缓存区以 buffer 形式存在。大多数情况下这是最后一步输出操作所期望的结果。如果还没结束输出,那可直接用 \n

相关资源

geeksforgeeks.org/endl-vs-n-in-cpp

原文链接: https://www.cnblogs.com/Wayou/p/cpp_endl_and_linebreak.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++ `endl` 与 `\n` 的区别

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/296715

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午6:08
下一篇 2023年2月15日 下午6:08

相关推荐