【问题标题】:how to pass by reference a struct for binary search C++如何通过引用传递二进制搜索C ++的结构
【发布时间】:2016-01-20 14:43:26
【问题描述】:
#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 right 和 int left 转到 st ,这是函数中的struct student 我不知道下一步该做什么。 任何帮助将不胜感激

【问题讨论】:

  • 因为 bin 搜索是有几个“逐个”的算法,最简单的方法是检查一些已经实现的。这是我通常复制的内容:lxr.free-electrons.com/source/lib/bsearch.c(通过发布此内容,我并不是说“复制代码”,而是逐行检查并使用它只是为了避免“关闭一个”错误。
  • 您的代码几乎不是 C++。摆脱那些指针,使用 std::string 并忘记 C 中的 typedef struct 技巧。
  • 您似乎在c++ 标头中编写c 代码。这就是为什么您不使用指针而不是引用的原因。我很想在答案中解释这一点,但我需要彻底改变你的代码
  • 这不是 C++ !!!!!!另外,与 C 不同的是,C++ 中的引用传递不是通过指针传递的。C++ 有一个不同的实体,称为引用。你的代码需要大量修改!!!
  • 你在谈论哪个传递引用?清楚

标签: c++ struct binary parameter-passing pass-by-reference


【解决方案1】:

s 的有效索引应该是从 0 到 n-1。所以“right = s[n].AM”试图访问超出分配内存的末尾。

【讨论】:

  • 当它询问如何通过引用传递并且您的更改未更新代码以使其通过引用传递时,您如何回答这个问题? -1
【解决方案2】:

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

【讨论】:

  • 当它询问如何通过引用传递并且您的更改未更新代码以使其通过引用传递时,您如何回答这个问题? -1
【解决方案3】:
int x, i, left = s[0].AM, right = s[n].AM, middle; // the error is somewhere here

这里有几个变化:

  • 无需再次声明变量 i,如果出于某种目的需要该变量,请考虑更改其名称。

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

【讨论】:

  • 当它询问如何通过引用传递并且您的更改没有更新代码以使其通过引用传递时,您如何回答这个问题? -1
猜你喜欢
  • 2013-05-12
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-13
  • 2016-01-19
相关资源
最近更新 更多