简体   繁体   English

递增/递减混淆

[英]Increment/Decrement Confusion

What is happening when we decrement the code here:当我们在这里递减代码时会发生什么:

temp[--countArray[getDigit(position, input[tempIndex], radix)]]

If temp is 1 in this case: are we decrementing first so that we are assigning to 0?如果在这种情况下 temp 为 1:我们是否先递减以便分配为 0? How immediate is this decrement?这种减少有多直接? It always seems to confuse me within array brackets.它似乎总是让我在数组括号内感到困惑。

Try unpacking the brackets on different levels of indentation:尝试拆开不同缩进级别的括号:

temp[                                                // get this index in temp
    --                                               // decrement by 1
    countArray[                                      // get this index in countArray
        getDigit(position, input[tempIndex], radix)  // call getDigit()
    ]
]

In human-readable terms, it calls getDigit() to index into countArray , then decrements that value and uses it to index into temp .用人类可读的术语来说,它调用getDigit()来索引countArray ,然后递减该值并使用它来索引temp


The decrement operator --x is different from x-- because of what it returns.递减运算符--xx--不同,因为它返回的内容不同。 By the end of the operation, x always ends up as one less than it was, but --x returns the new value of x , while x-- returns the old value of x from before it was decremented.在操作结束时, x总是比原来小 1,但--x返回x值,而x--返回x递减之前的值。 The same applies for ++x and x++ .这同样适用于++xx++

Let me break this down some.让我把它分解一些。 Here's some code that's equivalent to above:下面是一些与上面等效的代码:

int digit = getDigit(position, input[tempIndex], radix);
countArray[digit]--;
int count = countArray[digit];
temp[count] // Do something with the value

Incidentally, this is a classic illustration of why you shouldn't sacrifice clarity to brevity.顺便说一下,这是一个经典的例子,说明为什么不应该为了简洁而牺牲清晰度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM