C++ 如何通过引用传递用于二进制搜索的结构C++; #包括 #包括 使用名称空间std; 类型定义结构学生 { 字符名[30]; 查氏[50]; 国际调幅; 智力年龄; }st; int-binsearch(st*s,int-left,int-right,int-x)//当我尝试进行搜索时,程序崩溃 { while(左s[中].AM) 左=中+1; 其他的 右=中-1; } 返回-1; } int main() { int ap,flag=1,n,i=0; 圣路易斯; while(ap!=0) { couts[i].AM>>s[i].年龄; i++; } flag=2; } 如果(ap==2&&flag==2) { 对于(i=0;iP>不需要再次声明变量i,如果需要某个变量,请考虑更改它的名称。

C++ 如何通过引用传递用于二进制搜索的结构C++; #包括 #包括 使用名称空间std; 类型定义结构学生 { 字符名[30]; 查氏[50]; 国际调幅; 智力年龄; }st; int-binsearch(st*s,int-left,int-right,int-x)//当我尝试进行搜索时,程序崩溃 { while(左s[中].AM) 左=中+1; 其他的 右=中-1; } 返回-1; } int main() { int ap,flag=1,n,i=0; 圣路易斯; while(ap!=0) { couts[i].AM>>s[i].年龄; i++; } flag=2; } 如果(ap==2&&flag==2) { 对于(i=0;iP>不需要再次声明变量i,如果需要某个变量,请考虑更改它的名称。,c++,struct,binary,parameter-passing,pass-by-reference,C++,Struct,Binary,Parameter Passing,Pass By Reference,s的有效索引应该是从0到n-1。因此“right=s[n].AM”正在尝试访问超出分配内存的部分。在您注释的行中,(int x,i,left=s[0]。AM,right=s[n].AM,middle;)它应该是s[n-1]。AM因为大小为n的数组的索引只有从0到n-1 这里有几个变化: >P>不需要再次声明变量i,如果需要某个变量,请考虑更改它的名称。 s[n]应该是s[n-1] 因为bin搜索是一种包含多个“逐个关闭”的算法,所以最简单的方法是检查一些已经实现的算法。这是我通常复制的内容:

s的有效索引应该是从0到n-1。因此“right=s[n].AM”正在尝试访问超出分配内存的部分。

在您注释的行中,(int x,i,left=s[0]。AM,right=s[n].AM,middle;)它应该是s[n-1]。AM因为大小为n的数组的索引只有从0到n-1

这里有几个变化:

    >P>不需要再次声明变量i,如果需要某个变量,请考虑更改它的名称。

  • s[n]应该是s[n-1]


因为bin搜索是一种包含多个“逐个关闭”的算法,所以最简单的方法是检查一些已经实现的算法。这是我通常复制的内容:(通过发布此内容,我不会说“复制代码”,而是逐行检查,只是为了避免“逐个关闭”错误。你的代码几乎不存在C++。去掉这些指针,使用<代码> STD::String ,忘记C.的“代码”> TyBuffFrase< /Cord>技巧。你似乎正在写标题中的代码。这就是为什么你不使用指针而不是引用的原因。我很想在答案中解释这个问题,但是我需要彻底改变你的代码。SAMEE不是C++,加法引用不是C++中的指针,不像C++中有一个不同的实体叫做引用。你的代码需要很多修改!!!它是通过引用传递的吗?-1当它询问如何通过引用传递,而您的更改没有更新代码以使其通过引用时,您如何回答这个问题?-1当它询问如何通过引用传递,而您的更改没有更新代码以使其通过引用时,您如何回答这个问题?-1
#include <iostream>
#include <fstream>
using namespace std;

typedef struct student
{
    char name[30];
    char surname[50];
    int AM;
    int age;
}st;

int binsearch(st *s, int left, int right, int x) // the program crashes when i try to do the search                     
{
    while (left <= right)
    {
        int middle = (left +right) / 2;
        if (s[middle].AM == x)
            return middle;
        else if (x > s[middle].AM)
            left = middle + 1;
        else
            right = middle - 1;
    }
    return -1;
}

int main ()
{
    int ap,flag=1,n,i=0;

    st *s;
    while(ap != 0)
    {
       cout << "\n1. Create array" << endl;
       cout << "2. Print the array" << endl;
       cout << "3. Binary Search " << endl;
       cout << "0. Exit" << endl;
       cout << "Please give an answer ";
       cin >> ap;

       if (ap == 1)
       {

          ifstream file1("students.txt");
          if (!file1)
          {
          cout << "Unable to open file for reading.\n";
          return(1);
          }
          file1 >> n;
          s = new st[n];
          while (i < n)
          {
             file1 >> s[i].name  >> s[i].surname >> s[i].AM >> s[i].age;
             i++;
          }
          flag = 2;

       }
       if (ap == 2 && flag == 2)
       {
          for(i = 0; i < n; i++)
          {
              cout << s[i].name << " "  << s[i].surname << " " << s[i].AM << " " << s[i].age << " " << endl;
          }
       }
       if (ap == 3 && flag == 2)
       {
           int x, i, left = s[0].AM, right = s[n].AM, middle; // the error is somewhere here 

           cout << "Give the AM to search  "; // AM is a single number something like an ID number
           cin >> x;c 
           middle = binsearch(s, left, right, x);
           if(middle != -1)
           {
               cout << "We have found the student !" << endl;
           }
           else
           {
               cout << "We didn't find the student  %d" << x << endl;
           }

       }
    };
    delete[] s;
    return 0;
}
int x, i, left = s[0].AM, right = s[n].AM, middle; // the error is somewhere here