我++ VS ++作为我操作(无环) [英] i++ vs ++i as operation (NO loop)

查看:105
本文介绍了我++ VS ++作为我操作(无环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何人能帮助我理解之间的不同:

Does any one can help me with understand the different between:

i++;
++i;

我已经知道我++不先手术,之后我递增,与++ i,增量和不操作(内循环的意思)了。

I already know that i++ first does the operation and after that increments i, and ++i, increments and after that does the operation (the meaning inside loops).

但是,当这些操作的不可以(!)在循环中,而不是被分配/使用的,有什么区别?

But when these operations are not (!) inside a loop and not being assigned / used, what is the difference?

推荐答案

您似乎不感兴趣的时候不被随后使用差别,当一个循环里面没有,而是(这恰好是在循环中使用时,具有讽刺意味的​​情况下)。

You don't seem to be interested in the difference "when not inside a loop" but rather "when not being used afterwards" (which happens to be the case when used inside a loop, ironically).

因此​​,时不使用结果

i++;

基本上是*同

++i;

既是报表增加 I 一。

*)假设 I 为基本类型的语言。在Java中,这始终是,除非他们介绍运算符重载的情况。然而,在C ++中,在运算符可以重载(你的自定义类型定义),它完全依赖于变量类型的运算符重载执行我。大多数的实现是一致的,其结果将是相同的期望,当递增后,必须有参与的临时副本,其中的的事实可能的不会被优化掉了。在某些情况下,甚至有没有定义递增运算符重载,所以你必须写 ++我。而大多数开发商倾向于写这篇每默认情况下,即使是整数,在循环或外部循环。

*) Assuming i is a primitive type of the language. In Java, this is always the case unless they introduce operator overloading. However in C++, where operators can be overloaded (defined for your custom types), it depends completely on the implementation of operator overloads of the type of the variable i. Most implementations are consistent and the result will be the same expect the fact that when post-incrementing there has to be a temporary copy involved, which might not get optimized out. In some cases, there even is no post-increment operator overload defined, so you have to write ++i. And most developers tend to write this per default, even for integers, in loops or outside loops.

这篇关于我++ VS ++作为我操作(无环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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