目标 C,n++ 之间的差异和++n
在 Objective-C 中,n++ 和 ++n 之间有什么区别(例如,在 for 循环中使用)?
In Objective-C, is there any difference between n++ and ++n (eg. used in a for loop)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
++n;
在计算表达式之前递增n
的值。n++;
在计算表达式之后增加n
的值。因此,将其结果
与以下结果进行比较:
在循环的情况下:
但是它没有任何区别,除非您有这样的结果:
或这样:
人们可以说:
显然,相同的规则适用于
--n;
和n--;
。++n;
increments the value ofn
before the expression is evaluated.n++;
increments the value ofn
after the expression is evaluated.So compare the results of this
with the results of this:
In the case of loops:
however it doesn't make any difference, unless you had something like this:
or this:
One could say:
The same rules apply to
--n;
andn--;
, obviously.++n 在使用前递增值(前递增),n++ 在使用后递增(后递增)。
在 for 循环的上下文中,没有明显的差异,因为增量是在执行循环中的代码之后应用的。
++n increments the value before it's used (pre-increment) and n++ increments after (post-increment).
In the context of a for loop, there is no observable difference, as the increment is applied after the code in the loop has been executed.
++n
和n++
的不同之处在于表达式的计算结果。一个例子:++n
andn++
differ in what the expression evaluates to. An example:在循环中它不会产生任何影响。有些人说
++n
更快In a loop it wont make a difference. Some people say
++n
is faster though在 Scott Meyers 的《更有效的 C++》一书中,他提出了一个非常合理的理由,即优先选择前缀增量而不是后缀增量。简而言之,在该语言中,由于运算符重载设施,前缀增量几乎总是更快。 Objective C 不支持重载运算符,但如果您曾经或曾经做过任何 C++ 或 Objective-C++ 编程,那么首选前缀增量是一个好习惯。
请记住,大多数情况下 ++n 看起来像:
而 n++ 看起来像(如果按预期使用):
正如您所看到的后缀情况有更多指令。在简单的 for 循环中,如果很明显不会使用预增量 n 但这种情况会转移到前缀情况,大多数编译器都足够聪明,可以避免复制。
我希望这是有道理的。总之,除非您确实想要从后缀版本获得评估然后增量的“副作用”行为,否则您应该使用前缀。
In Scott Meyers "More Effective C++" Book he makes a very rational case for preferring prefix increment to postfix increment. In a nutshell, in that language due to operator overloading facilities prefix increment is almost always faster. Objective C doesn't support overloaded operators but if you have or ever will do any C++ or Objective-C++ programming then preferring prefix increment is a good habit to get into.
Remember that most of the time ++n looks like:
Whereas n++ looks like (if used as intended):
As you can see the postfix case has more instructions. In simple for loops most compilers are smart enough to avoid the copy if it's obvious that the pre-increment n isn't going to be used but that case devolves to the prefix case.
I Hope this makes sense. In summary you should use prefix unless you really want the "side-effect" behavior of evaluate then increment that you get from the postfix version.
如上所述,
--n 在计算表达式之前递减 n 的值。
n--; 在计算表达式后递减 n 的值。
这里需要注意的是使用 while 循环时
例如:
如 n-- 循环在 n = 1 时运行额外的时间
但在 --n 中,1 首先减为 0,然后再求值。这会导致 while 循环中断。
As stated above,
--n decrements the value of n before the expression is evaluated.
n--; decrements the value of n after the expression is evaluated.
The thing here to note is when using while loops
For example:
As in n-- the loop runs extra time while n = 1
But in --n 1 is first decremented to 0, and then evaluated. This causes the while loop to break.