为什么 C++在这个简单的程序中,编译器没有给出优先级(赋值下的递增运算符)?

发布于 2024-12-19 10:15:31 字数 683 浏览 0 评论 0原文

根据C/C++语言中运算符的优先级表(参见维基百科),自增运算符(++)优先于赋值运算符(=)。

有人可以解释一下为什么编译器首先在这个简单的程序中赋值(bill[x] 中的 1),然后增加索引值(i++)。我觉得应该是相反的(先增加再赋值):

#include <iostream>
using namespace std;

int bill[] = {16, 17, 18, 19, 20};

int main ()
{
  int i = 3;

  bill[(i++)] = 1; // I think it should be bill[4] = 1;

  cout << bill[0] << endl;
  cout << bill[1] << endl;
  cout << bill[2] << endl;
  cout << bill[3] << endl;
  cout << bill[4] << endl;

  cout << "Index value:" << i << endl;

  return 0;
}

输出是:

16
17
18
1
20
Index value:4

我做错了什么?

According to the table of precedence of operators in C/C++ language (see Wikipedia), the increment operator (++) takes precedence with respect to the assignment operator (=).

Can someone explain why the compiler first assign the value (1 in bill[x]) and then increases the index value (i++) in this simple program. I think it should be the opposite (first increase and then assign):

#include <iostream>
using namespace std;

int bill[] = {16, 17, 18, 19, 20};

int main ()
{
  int i = 3;

  bill[(i++)] = 1; // I think it should be bill[4] = 1;

  cout << bill[0] << endl;
  cout << bill[1] << endl;
  cout << bill[2] << endl;
  cout << bill[3] << endl;
  cout << bill[4] << endl;

  cout << "Index value:" << i << endl;

  return 0;
}

The output is:

16
17
18
1
20
Index value:4

I'm doing something wrong?

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

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

发布评论

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

评论(4

空城之時有危險 2024-12-26 10:15:31

i 正在递增,但不是在它用作数组访问器之前。要获得您正在寻找的内容,请尝试使用 ++i。 (前缀而不是后缀。)

i is being incremented, but not before it is used as the array accessor. To get what you're looking for, try ++i instead. (Prefix instead of postfix.)

我不咬妳我踢妳 2024-12-26 10:15:31

您可以用另一种方式来看待这个问题:

bill[(++i)] = 1;

您可以将其读作先递增“i”,然后执行该语句。

bill[(i++)] = 1;

您可以将其理解为,首先执行语句,然后递增“i”。

如果您想知道这是如何实现的,可以像这样实现内部后增量以获得您所看到的行为:

int post_increment(int &i)
{
  int t = i;
  i = i + 1;
  return t;
}

bill[post_increment(i)] = 1;    // access bill[3] even though i == 4

与预增量相比,如下所示:

int pre_increment(int &i)
{
  i = i + 1;
  return i;
}

bill[pre_increment(i)] = 1;    // access bill[4] where i == 4

Another way you can look at this:

bill[(++i)] = 1;

You can read it as, increment 'i' first then do the statement.

bill[(i++)] = 1;

You can read it as, first do the statement then increment 'i'.

If you're wondering how this is possible, internally post-increment can be implemented like this to get the behavior you're seeing:

int post_increment(int &i)
{
  int t = i;
  i = i + 1;
  return t;
}

bill[post_increment(i)] = 1;    // access bill[3] even though i == 4

vs pre-increment which looks like this:

int pre_increment(int &i)
{
  i = i + 1;
  return i;
}

bill[pre_increment(i)] = 1;    // access bill[4] where i == 4
如日中天 2024-12-26 10:15:31

“i++”的意思是,
“使用增量之前的变量值作为表达式结果,但增量变量”。

“++i”的意思是“增加变量,并使用增加后的值作为结果”。

"i++" means,
"use as the expression result, the variable value before the increment, but increment the variable".

"++i" means, "increment the variable, and use the incremented value as the result".

染火枫林 2024-12-26 10:15:31

首先完成增量。但是,i++ 会递增 i 并返回旧值的副本。正如其他人提到的,要获得所需的行为,请使用 ++i,它会递增 i 并返回对 i 的引用。

The increment is being done first. However, i++ increments i and returns a copy of the old value. As others have mentioned, to get the desired behaviour, use ++i, which increments i and returns a reference to i.

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