“std::endl"与“ " [英] "std::endl" vs " "

查看:39
本文介绍了“std::endl"与“ "的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多 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
";

是否有技术上的原因选择一种而不是另一种,还是仅仅是编码风格的问题?

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 ' ' doesn't. If you don't want the buffer flushed frequently, use ' '. If you do (for example, if you want to get all the output, and the program is unstable), use std::endl.

这篇关于“std::endl"与“ "的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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