简体   繁体   English

c ++通过递归进行二分搜索

[英]c++ Binary search through recursion

I receive an error when the first recursice call is made, the error:第一次递归调用时我收到一个错误,错误:

Unhandled exception at 0x002A2E44 in rekBinSearch.exe: 0xC0000005: Access violation reading location 0x0000000A. rekBinSearch.exe 中 0x002A2E44 处未处理的异常:0xC0000005:访问冲突读取位置 0x0000000A。

It is caused by:它是由:

if ((*pEnd - pBegin) == 0) / There's only one element */ if ((*pEnd - pBegin) == 0) /只有一个元素 */

It seems that that when i set the new start- and end-adress, i do something wrong since these can't be read on the recursive call.似乎当我设置新的开始地址和结束地址时,我做错了,因为在递归调用中无法读取这些地址。 They are "set" by:它们通过以下方式“设置”:

find(x, (int*)pBegin, pMid); find(x, (int*)pBegin, pMid);

Full code:完整代码:

    bool find(const int x, const int* pBegin, const int* pEnd)
{
   if ((*pEnd - *pBegin) == 0) /* There's only one element */
    {
        if (x == (int)pEnd)    /* That element could be the correct one */
            return true;
        else                   /* If it is not then return false, x is not in the array */
            return false;
    }

    int *pMid = (int*)(pEnd - pBegin);  /* pMid should be the adress to the element in the middle of the array */
    if (x >= (int)pMid)                 /* If x is in array it is to the right of the middle */
        find(x, (int*)pMid, pEnd);  
    else                                /* If x is in array it is to the left of the middle */           
        find(x, (int*)pBegin, pMid);

}// find

What am I doing wrong or how am I thinking wrong?我做错了什么或我怎么想错了?

What am I doing wrong or how am I thinking wrong?我做错了什么或我怎么想错了?

Problem 1问题一

You are confusing between pointers and values.您在指针和值之间感到困惑。 Examples:例子:

if ((*pEnd - *pBegin) == 0) /* There's only one element */

and

if (x == (int)pEnd)

int(pEnd) does not get the value of the object that pEnd points to. int(pEnd)没有得到pEnd指向的对象的值。 It just treats the pointer value as an int .它只是将指针值视为int

Problem 2问题二

Also, you are not returning properly from the recursive calls.此外,您没有从递归调用中正确返回。

    find(x, (int*)pMid, pEnd);  // Missing return

and

    find(x, (int*)pBegin, pMid); // Missing return

Fixed up function固定功能

Here's a version that should work.这是一个应该可以工作的版本。

bool find(const int x, const int* pBegin, const int* pEnd)
{
   if ((pEnd - pBegin) == 0) /* There's only one element */
   {
      return (x == *pEnd);  /* That element could be the correct one */
                            /* If it is not then return false, x is not in the array */
   }

   int midIndex = (pEnd - pBegin)/2;
   int const* pMid = pBegin + midIndex; /* pMid should be the adress to the element in the middle of the array */
   if (x >= *pMid)                     /* If x is in array it is to the right of the middle */
      return find(x, pMid, pEnd);  
   else                                /* If x is in array it is to the left of the middle */           
      return find(x, pBegin, pMid-1);

}// find

Do you want if ((pEnd - pBegin) == 0) ?你想要if ((pEnd - pBegin) == 0)吗? Notice that there is no dereference pointers.请注意,没有取消引用指针。 Dereferencing pend is always a bad idea since it doesn't point to anything.取消引用挂起总是一个坏主意,因为它不指向任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM