C++ 为什么SearchBack中的if(find==A[i])不是';在调试过程中,除了第一次,它显然不起作用

C++ 为什么SearchBack中的if(find==A[i])不是';在调试过程中,除了第一次,它显然不起作用,c++,C++,为什么在SearchBack中的if(find==A[i])除了第一次之外,没有计算为true #include <iostream> using namespace std; int Count(int find, int A[], int size); bool SearchBack(int A[], int size, int find, int in); int main() { int A[25] = { 1,2,1,85,14,7,2,2,14,

为什么在
SearchBack
中的
if(find==A[i])
除了第一次之外,没有计算为true

#include <iostream>

using namespace std;

int Count(int find, int A[], int size);
bool SearchBack(int A[], int size, int find, int in);

int main()

{
    int A[25] = 
    { 1,2,1,85,14,7,2,2,14,200,7,62,7,19,19,200,17,2,19,20,85,44,63,7,63 }, Q;
    for (int i = 0; i < 25; i++)
    {
        Q = 0;
        Q = Count(A[i], A, 25);
        if (Q != 0)
        {
            cout << A[i] << " : " << Q << endl;
        }
    }
    system("pause");
}

int Count(int find, int A[], int size)
{
    int counter = 0;
    bool N;
    for (int j = 0; j < size; j++)
    {
       if (find == A[j])
       {
            N = SearchBack(A, 25, A[j], j - 1);
            if (N == false)
            {
                counter++;
            }
            else
               return counter;
        }
        return counter;
     } 
 }

bool SearchBack(int A[], int size, int find, int in)
{  
    for (int i = 0; i <= in; i++) 
    {
        if (find == A[i])
            return true;
        else
            return false;
    }
}
#包括
使用名称空间std;
int Count(int find,int A[],int size);
bool SearchBack(int A[],int size,int find,int in);
int main()
{
INTA[25]=
{1,2,1,85,14,7,2,2,14200,7,62,7,19,19200,17,2,19,20,85,44,63,7,63},Q;
对于(int i=0;i<25;i++)
{
Q=0;
Q=计数(A[i],A,25);
如果(Q!=0)
{
cout基于“为什么除了第一次之外,SearchBack中的if(find==A[i])的计算结果不为真?”

SearchBack()函数没有问题。在
count()
函数中,我看到您将
返回计数器
置于if条件之后的for循环中,该条件仅在第一次迭代中终止循环

只需将return语句放在for循环之后:

int Count(int find, int A[], int size) {

    int counter = 0;

    bool N;

    for (int j = 0; j < size; j++)

     {
        if (find == A[j])

            {
                N = SearchBack(A, 25, A[j], j - 1);

                if (N == false)
                    {
                        counter++;
                    }
                else
                    return counter;
            }
    }
    return counter;

}
int计数(int find,int A[],int size){
int计数器=0;
布尔N;
对于(int j=0;j
有几件事你应该改变:

SearchBack()
中,只要
find
A[i]
不同,就可以返回,不要等待查看
find
是否出现在
A
的第一个元素之后。改为:

for (int i = 0; i < in; i++)  
    {  
        if (find == A[i])  
            return true;  
    }  
return false;

这样,您将只打印一次
A
的元素,并给出准确的出现次数。

请查看代码的渲染程度。您的格式错误,请编辑您的问题并修复它。提示:只需高亮显示一块代码,然后按Ctrl-K。
// in main
for (int i = 0; i < 25; i++)
    {
        Q = 0;
        Q = Count(A[i], A, 25, i);
...

// in Count, the current_element correspond to the 'i' above
N = SearchBack(A, 25, A[j], current_element);
...