目标 C,n++ 之间的差异和++n

发布于 2024-11-17 04:08:08 字数 57 浏览 1 评论 0原文

在 Objective-C 中,n++ 和 ++n 之间有什么区别(例如,在 for 循环中使用)?

In Objective-C, is there any difference between n++ and ++n (eg. used in a for loop)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

流云如水 2024-11-24 04:08:09

++n; 在计算表达式之前递增 n 的值。

n++; 在计算表达式之后增加n 的值。

因此,将其结果

int n = 41;
int o = ++n; //n = 42, o = 42

与以下结果进行比较:

int n = 41;
int o = n++; //n = 42, o = 41

在循环的情况下:

for (int i = 0; i < j; i++) {/*...*/}

但是它没有任何区别,除非您有这样的结果:

for (int i = 0; i < j; x = i++) {/*...*/}

或这样:

for (int i = 0; i < j; x = ++i) {/*...*/}

人们可以说:

只要同一表达式中没有修改第二个(相关)变量(基于 n),使用 n++ 还是 ++n 并不重要.


显然,相同的规则适用于 --n;n--;

++n; increments the value of n before the expression is evaluated.

n++; increments the value of n after the expression is evaluated.

So compare the results of this

int n = 41;
int o = ++n; //n = 42, o = 42

with the results of this:

int n = 41;
int o = n++; //n = 42, o = 41

In the case of loops:

for (int i = 0; i < j; i++) {/*...*/}

however it doesn't make any difference, unless you had something like this:

for (int i = 0; i < j; x = i++) {/*...*/}

or this:

for (int i = 0; i < j; x = ++i) {/*...*/}

One could say:

It doesn't matter whether to use n++ or ++n as long as no second (related) variable is modified (based on n) within the same expression.


The same rules apply to --n; and n--;, obviously.

放肆 2024-11-24 04:08:09

++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.

可可 2024-11-24 04:08:09

++nn++ 的不同之处在于表达式的计算结果。一个例子:

int n = 0;
NSLog(@"%d", n);   // 0
NSLog(@"%d", n++); // still 0, increments afterwards
NSLog(@"%d", n);   // 1
NSLog(@"%d", ++n); // 2, because it increments first
NSLog(@"%d", n);   // 2

++n and n++ differ in what the expression evaluates to. An example:

int n = 0;
NSLog(@"%d", n);   // 0
NSLog(@"%d", n++); // still 0, increments afterwards
NSLog(@"%d", n);   // 1
NSLog(@"%d", ++n); // 2, because it increments first
NSLog(@"%d", n);   // 2
万人眼中万个我 2024-11-24 04:08:09

在循环中它不会产生任何影响。有些人说 ++n 更快

In a loop it wont make a difference. Some people say ++n is faster though

时光倒影 2024-11-24 04:08:09

在 Scott Meyers 的《更有效的 C++》一书中,他提出了一个非常合理的理由,即优先选择前缀增量而不是后缀增量。简而言之,在该语言中,由于运算符重载设施,前缀增量几乎总是更快。 Objective C 不支持重载运算符,但如果您曾经或曾经做过任何 C++ 或 Objective-C++ 编程,那么首选前缀增量是一个好习惯。

请记住,大多数情况下 ++n 看起来像:

n = n + 1
[do something with n]

而 n++ 看起来像(如果按预期使用):

register A = n;  // copy n
[do something with n]
n = A + 1;

正如您所看到的后缀情况有更多指令。在简单的 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:

n = n + 1
[do something with n]

Whereas n++ looks like (if used as intended):

register A = n;  // copy n
[do something with n]
n = A + 1;

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.

眼波传意 2024-11-24 04:08:09

如上所述,
--n 在计算表达式之前递减 n 的值。

n--; 在计算表达式后递减 n 的值。

这里需要注意的是使用 while 循环时

例如:

n = 5
while(n--) #Runs the loop 5 times
while(--n) #Runs the loop 4 times

如 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:

n = 5
while(n--) #Runs the loop 5 times
while(--n) #Runs the loop 4 times

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文