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

Bru*_*rio 4 c++ compiler-construction operators operator-precedence

根据C/C++语言中运算符的优先级表(参见Wikipedia),增量运算符(++)优先于赋值运算符(=).

有人可以解释为什么编译器首先分配值(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;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

16
17
18
1
20
Index value:4
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

zie*_*mer 7

i正在递增,但在它被用作数组访问器之前.要获得你想要的东西,试试`++ i'.(前缀而不是后缀.)