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

发布于 2024-09-05 21:45:07 字数 153 浏览 6 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(8

找个人就嫁了吧 2024-09-12 21:45:07

i++ 返回一个值,因为它返回 i值,而 i 增加了 1..

其基本实现是:

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

因此,如果它返回一个引用,那么它将是错误的值...因为i的值已经递增!

i++ returns a value because it is returns the old value of i, while i is increased by 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!

尘世孤行 2024-09-12 21:45:07

++i 可以写成 因为

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

返回了真正的i,所以我们可以引用它。然而,i++看起来像

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

old_this只是一个局部变量,在i++完成后对其的引用是没有意义的。所以从逻辑上讲它应该返回一个右值。

++i can be written as

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

Since the real i is returned, we can take reference of it. However, i++ looks like

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. So logically it should return an rvalue.

灰色世界里的红玫瑰 2024-09-12 21:45:07

foo 是某个函数。 foo(i++) 使用 i 的旧值调用 foo(i) 并递增 i,因此需要构建一个临时副本。 foo(++i) 递增 i,然后使用递增的值调用 foo,因此为了获得更好的性能,我们可以重用相同的变量,不需要有一个临时副本。

Let foo be some function. foo(i++) calls foo(i) with the old value of i and increments i, hence the need to build a temporary copy. 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.

看轻我的陪伴 2024-09-12 21:45:07

我++
这将返回 i 递增之前的值。所以我们的想法是,如果你想在函数中使用 i,然后在使用它后增加值,你可以一步完成。
作为示例,以下是我如何重载整数运算符。

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

因此它会增加该值,然后返回它曾经的值。它也不会返回引用,因为返回的引用与最初传递的引用不同,这会破坏级联。

++i
这会增加 i 的值,然后返回新值。因此,您可以在想要增加 i 然后在函数中使用新值的情况下使用它。

Integer Integer::operator++(Integer i)
{
    i.increment();
    return 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.

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. So you could use this in a situation where you want to increment i and then use the new value in your function.

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

So the value it returns is the incremented value of i.

入怼 2024-09-12 21:45:07
int i = 0;
Console.Writeline(i++); // Output 0, after that, i will be 1


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

注意:代码是C#

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#

悸初 2024-09-12 21:45:07

虽然前缀 ++i 返回递增的值,而后缀 i++ 返回旧值并随后递增,如果您关心 CPU 周期,则运算符选择非常重要。前缀增量更快;-)

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. Prefix increment is faster ;-)

浅浅 2024-09-12 21:45:07

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 of for(...;...;i++), although compiller should optimize that.

未央 2024-09-12 21:45:07

如果你曾经处于一个重要的场景,那么你就做错了。

If you're ever in a scenario where it matters, you're doing it wrong.

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