簡體   English   中英

遞增/遞減混淆

[英]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


遞減運算符--xx--不同,因為它返回的內容不同。 在操作結束時, x總是比原來小 1,但--x返回x值,而x--返回x遞減之前的值。 這同樣適用於++xx++

讓我把它分解一些。 下面是一些與上面等效的代碼:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM