🌜

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

posted @ 2019-06-14 23:13  刘哇勇  阅读(1169)  评论(0编辑  收藏  举报

Bingo!!

少年,我看你骨骼清奇,怕是一名前端吧‽

腾讯内推长期有效,简历这边来 liuwayong@gmail.com