问题描述
temp[--countArray[getDigit(position,input[tempIndex],radix)]]
在这种情况下,如果temp为1:我们是先递减,然后赋值为0吗?这种减量有多快?总是让我在数组括号内感到困惑。
解决方法
尝试打开不同压痕级别的支架:
temp[ // get this index in temp
-- // decrement by 1
countArray[ // get this index in countArray
getDigit(position,input[tempIndex],radix) // call getDigit()
]
]
以易于理解的术语,它调用getDigit()
索引到countArray
,然后递减该值并将其用于索引temp
。
减量运算符--x
与x--
有所不同,因为它返回了什么。在操作结束时,x
总是比原来少一,但是--x
返回x
的 new 值,而{{1 }}从减小之前返回x--
的 old 值。 x
和++x
也是如此。
让我分解一下。这是与上面等效的一些代码:
int digit = getDigit(position,radix);
countArray[digit]--;
int count = countArray[digit];
temp[count] // Do something with the value
顺便说一下,这是为什么您不应该为了简洁而牺牲清晰度的经典例证。