简体   繁体   English

(++ i)和(i ++)之间的区别

[英]Difference between (++i) and (i++)

In C++ I understand that (++i) should return a reference to i because the need of concatenation of operators, but what I can't figure out is: 在C ++中,我理解(++i)应该返回对i的引用,因为需要连接运算符,但我无法弄清楚:

Why (i++) should return i by value? 为什么(i++)应该按值返回i

Can anyone please clarify. 任何人都可以澄清一下。

i++ returns a value because it is returns the old value of i , while i is increased by 1 . i++返回一个值,因为它返回i值,而i增加1

A basic implementation of this would be: 这个的基本实现是:

int i++() {
  int old = i;
  i = i + 1;
  return old;
}

So, if it returned a reference, it would be the wrong value ... since i 's value has been incremented! 所以,如果它返回一个引用,它将是错误的值 ......因为i的值已经增加了!

++i can be written as ++i可以写成

prefix_inc (this) {
   increase this by 1
   return this
}

Since the real i is returned, we can take reference of it. 由于返回了真实的i ,我们可以参考它。 However, i++ looks like 但是, i++看起来像

postfix_inc (this) {
   set old_this = copy of this
   increase this by 1
   return old_this
}

as old_this is just a local variable, the reference of it is pointless after i++ is completed. 因为old_this只是一个局部变量,所以在完成i++之后它的引用是没有意义的。 So logically it should return an rvalue. 所以逻辑上它应该返回一个右值。

Let foo be some function. foo成为一些功能。 foo(i++) calls foo(i) with the old value of i and increments i , hence the need to build a temporary copy. foo(i++)使用旧的i值调用foo(i)并递增i ,因此需要构建临时副本。 foo(++i) increments i and then calls foo with the incremented value, so for better performance we can reuse the same variable, no need to have a temporary copy. foo(++i)递增i ,然后使用递增的值调用foo ,因此为了获得更好的性能,我们可以重用相同的变量,不需要临时副本。

i++ This returns the value of the i before it is incremented. i ++这会在i递增之前返回i的值。 So the idea is that if you want to use i in a function, and then increment the value after using it, you can do that in one step. 所以我的想法是,如果你想在一个函数中使用i,然后在使用它之后增加它,你可以一步完成。 As an example, here is how I would overload that operator for integers. 作为一个例子,这是我如何重置该运算符的整数。

Integer Integer::operator++()
{
    Integer returnValue = *this;
    this->increment();
    return returnValue;
}

So it increments the value and then returns what it used to be. 所以它递增值然后返回它曾经是的。 It also doesn't return a reference, because returning a reference would be different from what was originally passed, which would break cascading. 它也不返回引用,因为返回引用将与最初传递的引用不同,这会破坏级联。

++i This increments the value of i, and then returns the new value. ++ i这会增加i的值,然后返回新值。 So you could use this in a situation where you want to increment i and then use the new value in your function. 因此,您可以在需要增加i然后在函数中使用新值的情况下使用它。

Integer Integer::operator++(Integer i)
{
    i.increment();
    return i;
}

So the value it returns is the incremented value of i. 所以它返回的值是i的递增值。

int i = 0;
Console.Writeline(i++); // Output 0, after that, i will be 1


int x = 0;
Console.Writeline(++x); // Output 1

Note: code is in C# 注意:代码在C#中

While prefix ++i returns the incremented value and the suffix i++ returns the old value and increments it afterwards the operator selection is significant if you care for the CPU cycles. 前缀++i返回递增的值,后缀i++返回旧值,然后递增,如果您关心CPU周期,则操作员选择很重要。 Prefix increment is faster ;-) 前缀增量更快;-)

5 cents: 5美分:

As a consequence of i++ making a copy, it is slower for non-POD variables (ie iterators). 由于i++制作副本,非POD变量(即迭代器)的速度较慢。 You should use ++i anywhere when possible. 你应该尽可能在任何地方使用++i

I personaly always use for(...;...;++i) instead of for(...;...;i++) , although compiller should optimize that. 我个人总是使用for(...;...;++i)而不是for(...;...;i++) ,尽管compiller应该优化它。

如果你曾经处于重要的场景中,那你就错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM