C ++:“std :: endl” vs“\\\<br/>” [英] C++: &quot;std::endl&quot; vs &quot;\n&quot;

查看:168
本文介绍了C ++:“std :: endl” vs“\\\<br/>”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多C ++书包含这样的示例代码...

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

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

...所以我也一直这样做。但我已经看到了很多来自这样的工作开发者的代码:

...so I've always done that too. But I've seen a lot of code from working developers like this instead:

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

有技术上的理由比一个更喜欢,还是只是一个编码风格?

Is there a technical reason to prefer one over the other, or is it just a matter of coding style?

推荐答案

不同的行结束字符无关紧要,假设文件在文本模式下打开,得到除非你要求二进制。编译好的程序将为系统编译的正确的东西。

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.

唯一的区别是 std :: endl 刷新输出缓冲区, '\\\
'
不会。如果不想频繁刷新缓冲区,请使用'\\\
'
。如果你这样做(例如,如果你想得到所有的输出,并且程序不稳定),使用 std :: endl

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.

这篇关于C ++:“std :: endl” vs“\\\<br/>”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆