当我们在这里减少代码时发生了什么:
temp[--countArray[getDigit(position, input[tempIndex], radix)]]
如果在本例中,temp为1:我们是否先递减,以便将其赋值为0?这个减幅有多快?似乎总是把我弄混在数组括号里。
发布于 2020-10-20 13:34:32
试着在不同的压痕水平上拆开括号:
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的新值,而x--返回减缩前的x的旧值。++x和x++也是如此。
发布于 2020-10-20 13:35:18
让我把这件事说几句。下面是一些与上面的代码等价的代码:
int digit = getDigit(position, input[tempIndex], radix);
countArray[digit]--;
int count = countArray[digit];
temp[count] // Do something with the value顺便说一句,这是一个经典的例子,说明了为什么你不应该为了简洁而牺牲清晰。
https://stackoverflow.com/questions/64446219
复制相似问题