的std :: ENDL [英] std::endl

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

问题描述

我在这个新闻组上看到很多帖子,人们使用std :: endl,但是我不知道为什么或从哪里得到它。 std :: endl输出一个新的
行并刷新流的缓冲区。在C ++ FAQ中它甚至是15.7。在

中,我的快速基准测试*''\ n''优于std :: endl超过4次

写入硬盘驱动器并且输入的字符少5个字符** :)。


那里有书籍告诉人们使用std :: endl吗?

预标准C ++不需要std :: endl来刷新流吗?


我希望这个咆哮可能会阻止一些人使用std :: endl除了

实际需要的地方因为它真的让我烦恼。


Michael Mellor


* g ++ 3.3 .1在Windows XP上的Cygwin下

**显然适当的使用会消除这种优势。

I see so many posts on this newsgroup where people use std::endl but I
can''t see why or where they are getting it from. std::endl outputs a new
line and flushes the stream''s buffer. It is even 15.7 in the C++ FAQ. In
my quick benchmarking* ''\n'' outperformed std::endl by over 4 times when
writing to the hard drive and is 5 less characters to type** :).

Are there books out there which tell people to use std::endl? Did
pre-standard C++ not require std::endl to flush the stream?

I hope this rant may stop a couple of people using std::endl except
where it is actually needed because it really annoys me.

Michael Mellor

*g++ 3.3.1 under Cygwin on Windows XP
**Obviously an appropriate using will remove that advantage.

推荐答案

"迈克尔梅勒" < news-at-@michaelmellor-dot-.com>在消息中写道:
"Michael Mellor" <news-at-@michaelmellor-dot-.com> wrote in message:
我在这个新闻组上看到很多帖子,人们使用std :: endl但是
我看不出他们为什么或从哪里得到它。 std :: endl输出一个
的新行并刷新流的缓冲区。在C ++
FAQ中它甚至是15.7。在我的快速基准测试中,'''\ n''在写入硬盘驱动器< snip>时超过std :: endl超过4倍



多快多少\ n比std :: endl更多关键在于什么

字符输出的百分比由换行符组成。您是否通过写新内容来比较两个

,或者您是否编写了一个样本

文本文档,例如网页?

那里有书籍告诉人们使用std :: endl吗?
预标准C ++不需要std :: endl来刷新流吗?


我想我已经看到它用在Hello World!中,但我不记得在哪里。

Stoustrup使用& ; \\\
" - 这应该不足为奇。

我希望这个咆哮可能会阻止一些人使用std :: endl,除非
实际需要它,因为它真的让我烦恼。
I see so many posts on this newsgroup where people use std::endl but I can''t see why or where they are getting it from. std::endl outputs a new line and flushes the stream''s buffer. It is even 15.7 in the C++ FAQ. In my quick benchmarking* ''\n'' outperformed std::endl by over 4 times when writing to the hard drive <snip>
How much faster "\n" is than "std::endl" depends critically on what
percentage of character output consists of newlines. Did you compare
the two by writing nothing but newlines, or did you write a sample
text document, such as a web page?
Are there books out there which tell people to use std::endl? Did
pre-standard C++ not require std::endl to flush the stream?
I think I''ve seen it used in "Hello World!", but I don''t recall where.
Stoustrup uses "\n" -- which should come as no surprise.
I hope this rant may stop a couple of people using std::endl except
where it is actually needed because it really annoys me.




为什么这么烦恼你?这个新闻组的大多数帖子都是

简单的演示程序,性能无关紧要。也许海报

认为它更具可读性。


Jonathan



Why does it annoy you so much? Most of the posts to this newsgroup are
simple demo programs where performance doesn''t matter. Maybe posters
think it''s more readable.

Jonathan


On Sun,25 2004年1月00:26:20 +0000,Michael Mellor< news-at-@michaelmellor-dot-.com>写道:
On Sun, 25 Jan 2004 00:26:20 +0000, Michael Mellor <news-at-@michaelmellor-dot-.com> wrote:
我在这个新闻组上看到很多帖子,人们使用std :: endl,但我不知道为什么或从哪里得到它。 std :: endl输出一个新的
行并刷新流的缓冲区。在C ++ FAQ中它甚至是15.7。在
我的快速基准测试*''\ n''在写入硬盘驱动器时超过std :: endl超过4次,并且键入**的字符少了5个。)。

那里有书籍告诉人们使用std :: endl吗?


例如,是的。


预标准C ++是否需要std :: endl来刷新流?


No.


我希望这个咆哮可能会阻止几个人使用std :: endl除了
它实际上是需要因为它真的让我烦恼。
I see so many posts on this newsgroup where people use std::endl but I
can''t see why or where they are getting it from. std::endl outputs a new
line and flushes the stream''s buffer. It is even 15.7 in the C++ FAQ. In
my quick benchmarking* ''\n'' outperformed std::endl by over 4 times when
writing to the hard drive and is 5 less characters to type** :).

Are there books out there which tell people to use std::endl?
By example, yes.

Did pre-standard C++ not require std::endl to flush the stream?
No.

I hope this rant may stop a couple of people using std::endl except
where it is actually needed because it really annoys me.




这是考虑不周的。


std :: endl有美德是* b $ b *清晰可读。

*健壮。


健壮意味着,如果应用程序崩溃,你仍然希望将最后一行写在磁盘上。


''\ n''可以习惯于_optimize_输出,但优秀的程序员会将这种优化推迟到需要的时间和地点,然后理想情况下只能在

分析之后。特别是因为它具有成本:降低可读性,在崩溃时甚至在某些情况下,例外情况下更少的坚固性。



That''s ill-considered.

std::endl has the virtues of being

* Clear and readable.
* Robust.

"Robust" means that if the app crashes you''ll still, hopefully, have the
last line written on disk.

''\n'' can be used to _optimize_ output, but good programmers defer such
optimization to when and where it''s needed, and then ideally only after
profiling. Especially since it has a cost: lowered readability, less
robustness in the face of crashes or even, in some cases, exceptions.


Alf P. Steinbach写道:
Alf P. Steinbach wrote:
On Sun,2004年1月25日00:26:20 +0000,Michael Mellor< news-at-@michaelmellor-dot-.com>写道:

On Sun, 25 Jan 2004 00:26:20 +0000, Michael Mellor <news-at-@michaelmellor-dot-.com> wrote:

我在这个新闻组上看到很多帖子,人们使用std :: endl,但是我不知道他们为什么或在哪里得到来自。 std :: endl输出一个新的
行并刷新流的缓冲区。在C ++ FAQ中它甚至是15.7。在
我的快速基准测试*''\ n''在写入硬盘驱动器时超过std :: endl超过4次,并且键入**的字符少了5个。)。

是否有书籍告诉人们使用std :: endl?

例如,是的。
I see so many posts on this newsgroup where people use std::endl but I
can''t see why or where they are getting it from. std::endl outputs a new
line and flushes the stream''s buffer. It is even 15.7 in the C++ FAQ. In
my quick benchmarking* ''\n'' outperformed std::endl by over 4 times when
writing to the hard drive and is 5 less characters to type** :).

Are there books out there which tell people to use std::endl?

By example, yes.
是否有预标准的C ++不要求std :: endl刷新流吗?
Did pre-standard C++ not require std::endl to flush the stream?



No.


No.

我希望这个咆哮可能会阻止一些人使用std: :endl除了
实际需要的地方因为它真的让我烦恼。
I hope this rant may stop a couple of people using std::endl except
where it is actually needed because it really annoys me.



这是考虑不周的。

std :: endl具有良好的优点

*清晰可读。
*健壮。


That''s ill-considered.

std::endl has the virtues of being

* Clear and readable.
* Robust.



我觉得它(endl)的可读性有点争论,'' \ n''对我和许多其他程序员来说意味着换行符

,如果没有,你可以做类似的事情:


static const char newline = ''\ n'';


stream<< "文本" <<换行;


可以声称更清晰。

健壮希望如果应用程序崩溃你仍然希望将最后一行写在磁盘上。


I feel its (endl) readability is somewhat arguable, ''\n'' means a newline
to me and many other programmers, and if not, you could do something like:

static const char newline = ''\n'';

stream << "text" << newline;

which could be claimed as being even clearer.
"Robust" means that if the app crashes you''ll still, hopefully, have the
last line written on disk.



但是如果你太担心使用大量缓冲区的话他们?当将二进制数据写入磁盘时,你是否明确刷新每个

记录/数据?


我看不到代码写作:

int a,b;

.....

std :: cout<< a<< std :: flush<< " " << std :: flush<< b<< std :: endl;

因为它效率低但它显然更强大。

我遇到的大多数C程序:

printf( a line\\\
);

printf(" another line \ nn);

.....

而不是


printf(a line\ n); fflush(stdout);

printf(" another line\\\
);; fflush(stdout);

.....


C ++程序员对崩溃更加偏执吗?也许C程序员

应该选择后一种方法。


Michael Mellor


But why have large buffers if you are too worried to use them? When
writing binary data to disk do you explicitly flush between every
record/piece of data?

I dont see code written as:
int a, b;
.....
std::cout << a << std::flush << " " << std::flush << b << std::endl;
because it is inefficient yet it is obviously more robust.
Most C programs I have come across:
printf("a line\n");
printf("another line\n");
.....

and not

printf("a line\n"); fflush(stdout);
printf("another line\n"); fflush(stdout);
.....

Are C++ programmers more paranoid about crashes? perhaps C programmers
should opt for the latter approach.

Michael Mellor


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

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