K&安培; R似乎preFER pre-增量 [英] K&R seems to prefer pre-increment

查看:171
本文介绍了K&安培; R似乎preFER pre-增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过K&放工作; R和上午psently $ P $上练习1-16。该迄今只有pre-增量已经在书中被用来它发生在我身上。

I'm working through K&R and am presently on Exercise 1-16. It occurs to me that thus far only pre-increment has been used in the book.

其他大部分教辅图书确实源$ C ​​$ C我见过倾向于后递增,除非有明显的影响,如while循环等。

Most other tutorial books and indeed source code I've seen tend to favour post-increment, except where there is an obvious affect such as in while loops etc.

这是一个风格或者技术考虑在K&放大器的部分; R?或者,我只是需要进一步通过这本书让我的答案吗?!

Is this a stylistic or technical consideration on the part of K&R? Or do I just need to be further through the book to get my answer?!

推荐答案

有几个方面需要这个。

语义

pre-递增(递减)和增量后(递减)的语义是不同的。而前者的增量用来值之前的值,后者增加其使用后的值。例如:

The semantics of pre-increment (decrement) and post-increment (decrement) is different. Whereas the former increments a value before the value is used, the latter increments a value after its use. For example

unsigned i;
for (i=0; i<10; i++ /* ++i */ ) { } 

在这种情况下,它不会不管一个人是否使用pre或后增。这使我的第二个方面:

In this case it would not matter whether one uses pre- or post-increment. Which brings me to the second aspect:

上下文

有时它无论使用pre或后增,这取决于所使用的上下文。请看下面的例子:

Sometimes it does matter whether to use pre- or post-increment, and that depends on the context of the use. Consider the following example:

char *p = "some string";
unsigned len = 0;

// test 1
while ('\0' != *p++) { len++; }

// test 2 (assumes that p points to a non-empty string)
while ('\0' != *++p) { ++len; }

的结果测试1 是字符串的长度,结果试验2 是长度串减一。由于增加值作为一个前pression的一部分 P ,它的确实的事情时,增量发生:使用后 p 在EX pression(测试1),或在使用前(测试2)。因为 LEN 不作为一名前pression的一部分,它的的事一个人是否使用pre型或后增量。

The result of test 1 is the length of the string, the result of test 2 is the length of the string minus one. Because the incremented value p is used as part of an expression, it does matter when the increment happens: after the use of p in the expression (test 1), or before its use (test 2). Because len is not used as part of an expression, it does not matter whether one uses pre- or post-increment.

这使我想到的第三个方面。

And that brings me to the third aspect.

实施

为了实现后递增, P 的值必须存放在远离其以后使用,以增加它,这会占用额外的存储空间为临时值。 $ P $对增量不需要临时存储空间。现代的优化编译器,但是,能够产生相同的code为pre型和后递增,如果增加值不作为Ex pression哪里会需要临时值的一部分以评估前pression。

In order to implement a post-increment, the value of p must be stored away for its later use to increment it, which takes up extra storage space for a temporary value. Pre-increment does not require temporary storage space. Modern optimizing compilers, however, are able to generate the same code for pre- and post-increment, if the incremented value is not used as part of an expression where the temporary value would be needed to evaluate that expression.

这使我第四方面(在在previous款有所暗示),但更多的是C ++相关。

Which brings me to the fourth aspect (somewhat hinted at in the previous paragraph), but that is more C++ related.

性能

在C ++中的递减和递增操作符可以被重载(链接)工作复杂的类型和STL(链接)确实让大量使用的,要实现迭代器(< A HREF =htt​​p://www.cplusplus.com/reference/iterator/相对=nofollow>链接)为其容器类型。在这种情况下,像一个值

In C++ the decrement and increment operators can be overloaded (link) to work with complex types, and STL (link) does make substantial use of that to implement iterators (link) for its container types. In that case, a value like

set::iterator it;

可以比琐碎状态更相当复杂的事情(即它不只是一种原始的整数)。在这种情况下,使用pre-增量 ++它确实的让它++ <过度后递增的差异/ code>因为没有必要像它是需要后增量如果在离pression用来存储临时值远(见上下文)。这可以节省相当长的一段运行时开销。

can be a rather complex thing with more than trivial state (i.e. it's not just a primitive integer). In that case, using pre-increment ++it does make a difference over post-increment it++ because there is no need to store a temporary value away like it is needed for post-increment if used in an expression (see Context). And that can save quite some runtime overhead.

这篇关于K&安培; R似乎preFER pre-增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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