[英]what is the diffrence between std::cout<< x ; and std::cout<<x<<std::endl;?
I'm a newbie to programming, I started teaching myself yesterday, I've been getting everything but I honestly, do not understand the difference between 我是编程的新手,我昨天开始自学,我已经得到了所有东西,但老实说,我不明白它们之间的区别
std::cout << x;
and 和
std::cout << x << std::endl;
Nobody has explained this to me, and I'm asking to stay on the safe side. 没有人向我解释这一点,我要求保持安全。
endl
writes a new-line to the stream, so subsequent output will appear on the next line. endl
将新行写入流,因此后续输出将显示在下一行。 It also flushes the stream's buffer, usually causing a slow-down . 它还会刷新流的缓冲区,通常会导致速度变慢 。
This flushing means that 99% of the time, endl
is a mistake, and you should just write "\\n"
(or '\\n'
) instead. 这种刷新意味着99%的时间,
endl
是一个错误,你应该只写"\\n"
(或'\\n'
)。 When you really do want to flush the stream, I think it's better to make that explicit by invoking std::flush
instead: 当你真的想要刷新流时,我认为最好通过调用
std::flush
来使其显式化:
std::cout << x << '\n' << std::flush;
As far as run-time actions goes, this is equivalent to using std::endl
, but in terms of making your intent clear, it's drastically superior. 就运行时操作而言,这相当于使用
std::endl
,但就使你的意图清晰而言,它是非常优越的。
The std::endl
adds a newline code to a stream and also flushes the output buffer and std::cout << x
is just printing x
. std::endl
为流添加换行代码并刷新输出缓冲区, std::cout << x
只是打印x
。 So if you got a code 所以,如果你有一个代码
cout << 5;
cout << 5;
it will be 这将是
55
55
as an output, but if you add a endl
to a first cout
the output will be 作为输出,但如果你将
endl
添加到第一个cout
,输出将是
5
五
5
五
What i really recommend you is to use '\\n'
it is much more better than endl. 我真正推荐你的是使用
'\\n'
它比endl更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.