[英]What is this java post increment operator doing?
So there's a piece of an radix sort implemented Java code that reads as below: 因此,有一个基数排序实现的Java代码,内容如下:
aux[count[a[i]]++] = a[i];
Why use a post increment operator? 为什么要使用后递增运算符? Why not aux[count[a[i]]+1] ?
为什么不选择aux [count [a [i]] + 1] ? Is the post increment simply to increment the value in count[a[i]] by 1 and store it there?
职位增值仅仅是将count [a [i]]中的值加1并将其存储在那里吗?
radixSort.java radixSort.java
int N = a.length;
int[] count = new int[R+1];
for (int i = 0; i < N; i++)
count[a[i]+1]++;
for (int r = 0; r < R; r++)
count[r+1] += count[r];
for (int i = 0; i < N; i++)
aux[count[a[i]]++] = a[i];
for (int i = 0; i < N; i++)
a[i] = aux[i];
Is the post increment simply to increment the value in count[a[i]] by 1 and store it there?
职位增值仅仅是将count [a [i]]中的值加1并将其存储在那里吗?
Yes, exactly. 对,就是这样。 There are two side-effects of the statement: one is a modification to an element of
aux
, and the other is a modification to an element in count
. 该语句有两个副作用:一个是对
aux
元素的修改,另一个是对count
元素的修改。
Personally I'd avoid writing it like that - I'd probably write: 我个人会避免这样写-我可能会写:
// We don't use i other than to index into a, so use an
// enhanced for loop instead
for (int value : a)
{
aux[count[value]] = value;
count[value]++;
}
Note that even if the change to count
weren't required, aux[count[a[i]]+1]
wouldn't do the same thing - because aux[count[a[i]]++]
refers to the element in aux
with index count[a[i]]
, before the increment, because ++
is being used as a post-increment here. 请注意,即使不需要更改
count
, aux[count[a[i]]+1]
也不会做同样的事情-因为aux[count[a[i]]++]
是指元素在带有索引count[a[i]]
aux
, 在递增之前 ,因为在这里++
被用作后递增。
This aux[count[a[i]]++] = a[i];
这个
aux[count[a[i]]++] = a[i];
means: 手段:
first, take a[i]
value, and use it as index for count. 首先,取
a[i]
值,并将其用作计数的索引。 Then that value use as index in aux
array and place the value of a[i]
to its place. 然后,该值用作
aux
数组中的索引,并将a[i]
的值放置在其位置。 Then increment the value on position count[a[i]]
for 1
. 然后将位置
count[a[i]]
上的值增加1
。
Its like this: 就像这样:
aux[count[a[i]]] = a[i];
count[a[i]] = count[a[i]] + 1;
but shorter. 但更短。
You have also this: aux[++count[a[i]]] = a[i];
您还具有以下内容:
aux[++count[a[i]]] = a[i];
. 。 This will take value from
count[a[i]]
and first increment for 1
and then use it as index in aux
array, so now we search it as: 这将从
count[a[i]]
获取值,并首先递增1
,然后将其用作aux
数组中的索引,所以现在将其搜索为:
count[a[i]] = count[a[i]] + 1;
aux[count[a[i]]] = a[i];
As you can see, aux[count[a[i]]+1]
is not like aux[count[a[i]]++]
, since it will not store increased value of a[i]
for 1 in a[i]
, and it will take index a[i] + 1
from aux
, but aux[count[a[i]]++]
takes index of a[i]
. 正如可以看到,
aux[count[a[i]]+1]
是不喜欢aux[count[a[i]]++]
的,因为它不会存储增加的值a[i]
在1 a[i]
,它将从aux
索引a[i] + 1
,但是aux[count[a[i]]++]
接受a[i]
索引。
aux[count[a[i]]+1]
is similar to aux[++count[a[i]]]
but the difference is you didn't increment the value in count[a[i]]
. aux[count[a[i]]+1]
与aux[++count[a[i]]]
类似,不同之处在于您没有增加count[a[i]]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.