(++i) 和 (i++) 之间的区别
在 C++ 中,我理解 (++i)
应该返回对 i
的引用,因为需要连接运算符,但我不明白的是:
为什么 < code>(i++) 应该按值返回 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:
Why (i++)
should return i
by value?
Can anyone please clarify.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
i++
返回一个值,因为它返回i
的旧值,而i
增加了1.
.其基本实现是:
因此,如果它返回一个引用,那么它将是错误的值...因为
i
的值已经递增!i++
returns a value because it is returns the old value ofi
, whilei
is increased by1
.A basic implementation of this would be:
So, if it returned a reference, it would be the wrong value... since
i
's value has been incremented!++i
可以写成 因为返回了真正的
i
,所以我们可以引用它。然而,i++
看起来像old_this
只是一个局部变量,在i++
完成后对其的引用是没有意义的。所以从逻辑上讲它应该返回一个右值。++i
can be written asSince the real
i
is returned, we can take reference of it. However,i++
looks likeas
old_this
is just a local variable, the reference of it is pointless afteri++
is completed. So logically it should return an rvalue.让
foo
是某个函数。foo(i++)
使用i
的旧值调用foo(i)
并递增i
,因此需要构建一个临时副本。foo(++i)
递增i
,然后使用递增的值调用foo
,因此为了获得更好的性能,我们可以重用相同的变量,不需要有一个临时副本。Let
foo
be some function.foo(i++)
callsfoo(i)
with the old value ofi
and incrementsi
, hence the need to build a temporary copy.foo(++i)
incrementsi
and then callsfoo
with the incremented value, so for better performance we can reuse the same variable, no need to have a temporary copy.我++
这将返回 i 递增之前的值。所以我们的想法是,如果你想在函数中使用 i,然后在使用它后增加值,你可以一步完成。
作为示例,以下是我如何重载整数运算符。
因此它会增加该值,然后返回它曾经的值。它也不会返回引用,因为返回的引用与最初传递的引用不同,这会破坏级联。
++i
这会增加 i 的值,然后返回新值。因此,您可以在想要增加 i 然后在函数中使用新值的情况下使用它。
所以它返回的值是 i 的增量值。
i++
This returns the value of the i before it is incremented. 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.
As an example, here is how I would overload that operator for integers.
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. So you could use this in a situation where you want to increment i and then use the new value in your function.
So the value it returns is the incremented value of i.
注意:代码是C#
Note: code is in C#
虽然前缀
++i
返回递增的值,而后缀i++
返回旧值并随后递增,如果您关心 CPU 周期,则运算符选择非常重要。前缀增量更快;-)While prefix
++i
returns the incremented value and the suffixi++
returns the old value and increments it afterwards the operator selection is significant if you care for the CPU cycles. Prefix increment is faster ;-)5 美分:
由于
i++
进行复制,非 POD 变量(即迭代器)速度较慢。您应该尽可能在任何地方使用++i
。我个人总是使用
for(...;...;++i)
而不是for(...;...;i++)
,尽管编译器应该优化那个。5 cents:
As a consequence of
i++
making a copy, it is slower for non-POD variables (i.e. iterators). You should use++i
anywhere when possible.I personaly always use
for(...;...;++i)
instead offor(...;...;i++)
, although compiller should optimize that.如果你曾经处于一个重要的场景,那么你就做错了。
If you're ever in a scenario where it matters, you're doing it wrong.