C++ 此声明计数的含义[array[i]]
有人能解释一下下面程序中的C++ 此声明计数的含义[array[i]],c++,c,C++,C,有人能解释一下下面程序中的count[array[i]]是什么意思吗? 该代码的目的是以一个数组的频率打印所有重复的数字 #include <stdio.h> #include <malloc.h> void duplicate(int array[], int num) { int *count = (int *)calloc(sizeof(int), (num - 2)); int i; printf("duplicate elements
count[array[i]]
是什么意思吗?
该代码的目的是以一个数组的频率打印所有重复的数字
#include <stdio.h>
#include <malloc.h>
void duplicate(int array[], int num)
{
int *count = (int *)calloc(sizeof(int), (num - 2));
int i;
printf("duplicate elements present in the given array are ");
for (i = 0; i < num; i++)
{
if (count[array[i]] == 1)
printf(" %d ", array[i]);
else
count[array[i]]++;
}
}
int main()
{
int array[] = {5, 10, 10, 2, 1, 4, 2};
int array_freq = sizeof(array) / sizeof(array[0]);
duplicate(array, array_freq);
getchar();
return 0;
}
#包括
#包括
无效重复(int数组[],int num)
{
int*count=(int*)calloc(sizeof(int),(num-2));
int i;
printf(“给定数组中存在的重复元素是”);
对于(i=0;i
这是在数组中查找重复项时使用的最糟糕的方法
count[array[i]]++;
因此,array[i]
将返回该索引处的数字,该数字将作为count数组的索引。例如:-
array[4] = {1,2,3,1};
将数组迭代到其中,如下所示:-
count[array[0]] = count[1] = 1;
count[array[1]] = count[2] = 1;
count[array[2]] = count[3] = 1;
count[array[3]] = count[1] = 2; << Increment the count...
count[array[0]]=count[1]=1;
计数[数组[1]]=计数[2]=1;
计数[数组[2]]=计数[3]=1;
计数[数组[3]]=计数[1]=2 如果我没弄错的话。您正在尝试打印给定数组中的所有副本。
首先,创建一个数组计数并用零填充它
int *count = (int *)calloc(sizeof(int), (num - 2));
如果计数[x]等于0,则表示数组中没有数字x。
如果count[x]等于1,则表示数组中只有一个x实例。
如果计数[x]大于1,则表示数组中有多个x实例
因此,您将遍历给定的数组并更新计数数组。在进行更新的同时,您正在检查是否存在任何重复项。这就是这些行的作用:
if (count[array[i]] == 1)
printf(" %d ", array[i]);
else
count[array[i]]++;
从我的观点来看,这不是做这样例行工作的最佳方式。我现在看到的两个问题是:不能释放count数组,并且count数组的大小必须大于给定数组中的任何数字。作为另一个解决方案,您尝试考虑如何使用std::set或std::unique函数完成此任务
希望有帮助。您显示的代码毫无意义。例如,functioncalloc
的第一个参数是必须分配的元素数。但是在代码中,相应的参数是sizeof(int)
,通常等于4。函数的第二个参数是元素的大小。但是在程序中指定了num-2
(?)。尽管calloc分配的内存范围等于第一个参数与第二个参数的乘积,但完全不清楚为什么会使用num-2
至于这个表达式count[array[i]
,那么它显然是一个bug。数组[i]的值可以远远大于动态内存中分配的元素数
因为很明显,您可以从逻辑上将这个表达式拆分为两个。比如说
int j = array[i];
count[j];
例如数组
int array[] = {5, 10, 10, 2, 1, 4, 2};
数组[1]
等于10。因此,count[array[i]
相当于count[10]
,但只分配了count指向的num-2
元素。由于在这种特殊情况下num-2
等于5,那么count[10]是数组中不存在的元素
考虑到在一般情况下,您需要空闲的动态分配内存
我想你的意思是这样的
#include <stdio.h>
#include <stdlib.h>
void duplicate( const int a[], size_t n )
{
size_t *count;
int i;
if ( n < 2 ) return;
count = ( size_t * )calloc( n - 1, sizeof( size_t ) );
for ( i = 1; i < n; i++ )
{
size_t j = 0;
while ( j < i && a[i] != a[j] ) j++;
if ( j != i ) ++count[j];
}
printf( "duplicate elements present in the given array are " );
for ( i = 0; i < n - 1; i++ )
{
if ( count[i] )
{
printf( " %d ", a[i] );
}
}
free( count );
}
int main(void)
{
int a[] = { 5, 10, 10, 2, 1, 4, 2 };
size_t n = sizeof( a ) / sizeof( *a );
duplicate( a, n );
return 0;
}
如果要替换声明
printf( " %d ", a[i] );
为了
然后输出将是
duplicate elements present in the given array are 10(2) 2(2)
如果您要使用我的代码,请不要忘记将我的答案标记为最佳。数组[i]
做什么?count[someIndex]
做什么?把它们放在一起。这也不是一个声明可以重写为:int t=array[i];计数[t]++代码>。它有用吗?它用于查找上次编辑时回滚的数组中的重复项。这个问题已经完全改变了。
printf( " %d(%zu) ", a[i], count[i] + 1 );
duplicate elements present in the given array are 10(2) 2(2)