void doSomething()
{
int hist[5] = { 0 };
int num_bins = 5;
int indices[10] = { 0, 0, 2, 3, 3, 3, 3, 4, 4, 4 };
int num_indices = 10;
int i;
for (i = 0; i < num_indices; i++)
{
hist[indices[i]]++;
}
for (i = 0; i < num_bins; i++)
{
printf("%d ", hist[i]);
}
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
假设我有正确的库,这是来自课堂的概念性问题.我想知道阵列的答案如何得出2 0 1 4 3.
这条线
hist[indices[i]]++
Run Code Online (Sandbox Code Playgroud)
说" hist在索引处转到数组的元素indices[i],然后递增它." 如果您将数组视为计数器列表,则表示将计数器递增到位indices[i].
此代码构建阵列中各种数字的频率的直方图.上述代码背后的想法是迭代数组并增加每个元素的频率.
希望这可以帮助!