[英]Increment/Decrement Confusion
当我们在这里递减代码时会发生什么:
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
总是比原来小 1,但--x
返回x
的新值,而x--
返回x
递减之前的旧值。 这同样适用于++x
和x++
。
让我把它分解一些。 下面是一些与上面等效的代码:
int digit = getDigit(position, input[tempIndex], radix);
countArray[digit]--;
int count = countArray[digit];
temp[count] // Do something with the value
顺便说一下,这是一个经典的例子,说明为什么不应该为了简洁而牺牲清晰度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.