C++数组,计数函数中的重复数 我一直在学习java的最近的C++,我试图计算数组中重复值的数目。由于某些原因,数组没有正确地传递给我的计数器函数 #include <iostream> #include <time.h> using namespace std; //quicksort for int arrays, left should be left index (0), right is right index(last one) void quSort(int input[], int left, int right); //binary search will return the index of the target or -1 if not found int biSearch(int input[], int target, int iLeft, int iRight); //count reapeats in the array with biSearch int countRepeats(int input[], int target); int main() { srand((unsigned int) time(0)); int test[1000]; //generate 100 random numbers under 1000 for(int i = 0; i < 1000; i++) test[i] = rand()%1000; //output test original cout << "orig: "; for(int i = 0; i < sizeof(test)/sizeof(*test); i++) { cout << test[i] << " "; } cout << endl << endl; //sorting quSort(test,0,( (sizeof(test)/sizeof(*test))-1)); cout << "sorted: "; for(int i = 0; i < sizeof(test)/sizeof(*test); i++) { cout << test[i] << " "; } //binary search test int target; int iTarget; cout << "\nenter target: "; cin >> target; iTarget = biSearch(test,target,0,sizeof(test)/sizeof(*test)); cout << "\n the target is at index: " << iTarget << " :: test[" << iTarget << "] = " << test[iTarget]; //count repeats cout << "\nWith " << countRepeats(test,target) << " repeats"; system("pause"); return 0; } //quicksort function; effiecent array sorter; important for furture array analysis!!! void quSort(int input[], int left, int right) { int pivot = input[(left+right)/2]; int l = left;//to control loop int r = right; while(l <= r)//will get smaller over iterations { int placeHold;// for use in swap, temp number //finds value higher than the pivot from left while(input[l] < pivot) l++; //find value lower than pivot on right while(input[r] > pivot) r--; //swapper if(l <= r) { //if the value greater than pivot is to the left of the value //lessser than pivot placeHold = input[l]; input[l] = input[r]; input[r] = placeHold; l++; r--; } //recursion to sort whole array until l=r if(left<r) quSort(input, left, r); if(l < right) quSort(input, l , right); } } //binary search function; array MUST be sorted int biSearch(int input[], int target, int iLeft, int iRight) { if(iLeft > iRight) return -1; else { int iMid = ((iLeft+iRight)/2); if(input[iMid] > target) return biSearch(input, target, iLeft, iMid-1); else if(input[iMid] < target) return biSearch(input, target, iMid+1, iRight); else return iMid;//target found } } //Must be sorted int countRepeats(int *input, int target) { int holder[sizeof(input)/sizeof(*input)]; int biSResult; int counter = 0; biSResult = biSearch(input,target,0,sizeof(input)/sizeof(*input)); //bug test cout<<"c++" << biSResult << "c++"; // while(biSResult != -1) { holder[biSResult] = target; counter++; input[biSResult] = 0; quSort(input,0,( (sizeof(input)/sizeof(*input))-1)); biSResult = biSearch(input,target,0,sizeof(input)/sizeof(*input)); } biSResult = biSearch(holder,target,0,sizeof(holder)/sizeof(*holder)); while(biSResult != -1) { input[biSResult] = target; holder[biSResult] = 0; quSort(holder,0,( (sizeof(holder)/sizeof(*holder))-1)); biSResult = biSearch(input,target,0,sizeof(holder)/sizeof(*holder)); } return counter; }

C++数组,计数函数中的重复数 我一直在学习java的最近的C++,我试图计算数组中重复值的数目。由于某些原因,数组没有正确地传递给我的计数器函数 #include <iostream> #include <time.h> using namespace std; //quicksort for int arrays, left should be left index (0), right is right index(last one) void quSort(int input[], int left, int right); //binary search will return the index of the target or -1 if not found int biSearch(int input[], int target, int iLeft, int iRight); //count reapeats in the array with biSearch int countRepeats(int input[], int target); int main() { srand((unsigned int) time(0)); int test[1000]; //generate 100 random numbers under 1000 for(int i = 0; i < 1000; i++) test[i] = rand()%1000; //output test original cout << "orig: "; for(int i = 0; i < sizeof(test)/sizeof(*test); i++) { cout << test[i] << " "; } cout << endl << endl; //sorting quSort(test,0,( (sizeof(test)/sizeof(*test))-1)); cout << "sorted: "; for(int i = 0; i < sizeof(test)/sizeof(*test); i++) { cout << test[i] << " "; } //binary search test int target; int iTarget; cout << "\nenter target: "; cin >> target; iTarget = biSearch(test,target,0,sizeof(test)/sizeof(*test)); cout << "\n the target is at index: " << iTarget << " :: test[" << iTarget << "] = " << test[iTarget]; //count repeats cout << "\nWith " << countRepeats(test,target) << " repeats"; system("pause"); return 0; } //quicksort function; effiecent array sorter; important for furture array analysis!!! void quSort(int input[], int left, int right) { int pivot = input[(left+right)/2]; int l = left;//to control loop int r = right; while(l <= r)//will get smaller over iterations { int placeHold;// for use in swap, temp number //finds value higher than the pivot from left while(input[l] < pivot) l++; //find value lower than pivot on right while(input[r] > pivot) r--; //swapper if(l <= r) { //if the value greater than pivot is to the left of the value //lessser than pivot placeHold = input[l]; input[l] = input[r]; input[r] = placeHold; l++; r--; } //recursion to sort whole array until l=r if(left<r) quSort(input, left, r); if(l < right) quSort(input, l , right); } } //binary search function; array MUST be sorted int biSearch(int input[], int target, int iLeft, int iRight) { if(iLeft > iRight) return -1; else { int iMid = ((iLeft+iRight)/2); if(input[iMid] > target) return biSearch(input, target, iLeft, iMid-1); else if(input[iMid] < target) return biSearch(input, target, iMid+1, iRight); else return iMid;//target found } } //Must be sorted int countRepeats(int *input, int target) { int holder[sizeof(input)/sizeof(*input)]; int biSResult; int counter = 0; biSResult = biSearch(input,target,0,sizeof(input)/sizeof(*input)); //bug test cout<<"c++" << biSResult << "c++"; // while(biSResult != -1) { holder[biSResult] = target; counter++; input[biSResult] = 0; quSort(input,0,( (sizeof(input)/sizeof(*input))-1)); biSResult = biSearch(input,target,0,sizeof(input)/sizeof(*input)); } biSResult = biSearch(holder,target,0,sizeof(holder)/sizeof(*holder)); while(biSResult != -1) { input[biSResult] = target; holder[biSResult] = 0; quSort(holder,0,( (sizeof(holder)/sizeof(*holder))-1)); biSResult = biSearch(input,target,0,sizeof(holder)/sizeof(*holder)); } return counter; },c++,arrays,sorting,search,count,C++,Arrays,Sorting,Search,Count,如果有人知道为什么会发生这种情况,那将是一个很大的帮助函数countRepeats的第一个参数声明为 int countRepeats(int *input, int target) int countRepeats(int *input, int n, int target); 就是说它有int类型* 所以 相当于 sizeof( int * )/sizeof( int ) 例如,如果sizeof int*等于4且sizeof int也等于4,则表达式将等于1。也就是说,表达式的值并不取

如果有人知道为什么会发生这种情况,那将是一个很大的帮助

函数countRepeats的第一个参数声明为

int countRepeats(int *input, int target)
int countRepeats(int *input, int n, int target);
就是说它有int类型*

所以

相当于

sizeof( int * )/sizeof( int )
例如,如果sizeof int*等于4且sizeof int也等于4,则表达式将等于1。也就是说,表达式的值并不取决于作为参数传递给函数的数组包含多少元素

您应该将数组的大小作为函数的参数显式传递。因此,th函数应该声明为

int countRepeats(int *input, int target)
int countRepeats(int *input, int n, int target);

或者,您可以将函数的第一个参数声明为对数组的引用。

在接收到数组指针的函数中,无法计算数组的大小。这是因为计数函数中的sizeofinput值将返回指向int的指针的大小。因此sizeofinput/sizeof*输入将始终为1

如果您计算数组的大小并将其作为int存储在main中,则将其传递给计数函数,它应该可以工作。因此,将计数函数调用更改为:


cout在重复中有几个问题:

1如其他答案中所述,参数传递方式存在错误。无论使用int*还是int[],都无法计算函数中数组的大小。因此,最好像在quSort中一样,通过不仅提供数组地址,还提供开始和结束计数器,将参数传递给该函数

2如果用户要求目标为0:your whilebeisult!=-我将永远循环

3此函数对数组进行一次又一次排序。这似乎对性能非常不利。为什么不利用数组已经排序的事实呢?您可以从找到的索引开始计数。想想看,你必须在这个位置前后数一数,因为你不确定iTarget是不是第一次出现。这可能看起来像:

int countRepeats(int input[], int pos, int start, int end )
{
    if (pos<start || pos>=end)  // you never know ! 
        return 0; 

    int counter = 1;

    for (int i=pos-1; i>=start && input[i]==input[pos]; i--)
        counter++; 
    for (int i=pos+1; i<end && input[i]==input[pos]; i++)
        counter++;

    return counter;
}
顺便说一句,我已经测试过了,它很有效。您只需调整原型,并使用

   cout << "\nWith " << countRepeats(test, iTarget, 0,
                    sizeof(test)/sizeof(*test) ) << " repeats";

请不要发布数百行要求我们查找问题的代码。相反,把它简化成一个说明问题的问题。请把一个代替一个代码墙。我意识到这不是你想听到的,但是至少在将来,你应该真的,真的,真正地考虑使用STD::数组或者更好的是STD::vector,而不是原始数组。将std::vector作为计数函数的常量引用传递给函数不会有任何问题。您可以使用std::vector、std::sort和std::equal_range。我正在尝试计算数组中重复值的数量,这可以使用std::map在不到10行代码中完成。