“std::endl” vs “\n”

后端 未结 12 2384
耶瑟儿~
耶瑟儿~ 2020-11-21 04:35

Many C++ books contain example code like this...

std::cout << \"Test line\" << std::endl;

...so I\'ve always done that too. But

12条回答
  •  一生所求
    2020-11-21 05:20

    The varying line-ending characters don't matter, assuming the file is open in text mode, which is what you get unless you ask for binary. The compiled program will write out the correct thing for the system compiled for.

    The only difference is that std::endl flushes the output buffer, and '\n' doesn't. If you don't want the buffer flushed frequently, use '\n'. If you do (for example, if you want to get all the output, and the program is unstable), use std::endl.

提交回复
热议问题