简体   繁体   English

递归函数的堆栈溢出错误

[英]Stack Overflow Error From Recursive Function

#include<iostream>
#include<string>
#include<sstream>
#include<conio.h>
#include<vector>
#define MAX 100
using namespace std;
int size;

int getlargest(int arr[ ]){
    static int max = -1000;
    static int i = 0;
    if(i < size){
        if(arr[i] >= max){
            max = arr[i];
            i++;
        }
        getlargest(arr);
    }
    return max; 
}

int main(){

    int res;
    cout << "enter the size please: ";
    cin >> size;
    int arr[MAX];

    for(int i=0;i<size;i++){
        cin >> arr[i];
    }
    res = getlargest(arr);
    cout << res;
    getch();
    return 0;
}

I am not experienced with the concept of recursive functions. 我对递归函数的概念没有经验。 This code was written to find the maximum element of an array. 编写此代码是为了查找数组的最大元素。 However, I am getting a stack overflow error. 但是,我收到堆栈溢出错误。 Could anyone correct it? 有人可以纠正吗? Also, I don't know exactly where to insert recursion. 另外,我不知道确切在哪里插入递归。

You have several problems, all of them small. 您有几个问题,都很小。

First, you make no progression through the array: you always call with the same argument, that being the entire array. 首先,您不会在数组上进行任何操作:始终使用相同的参数进行调用,即整个数组。 The strategy of recursion is to do something simple, and then reduce the problem to something smaller when you call again. 递归的策略是做一些简单的事情,然后在再次调用时将问题减小到较小的程度。

In this case, you have the concept right: check one element against the largest of the rest of the list. 在这种情况下,您拥有正确的概念 :对照列表中其余元素中的最大元素检查一个元素。 You do recur to find the maximum, but you don't reduce the list. 可以重复查找最大值,但不会减少列表。 You also don't actually work well with the list max. 您实际上也不能很好地使用list max。 For instance, note that (on each call) you're returning the max to the previous level ... but you don't save it at all. 例如,请注意(在每次调用中)您将最大值返回到上一个级别...但根本没有保存。

Try this instead: 尝试以下方法:

  • take the first element off the list; 从列表中删除第一个元素;
  • find the max of the remainder; 找到余数的最大值;
  • return the larger of those two. 返回这两个中的较大者。

The code might look like this: 代码可能看起来像这样:

if(arr.size() == 1) {
    return arr[0]
else {
    max = getlargest(++arr);
    if (arr[0] >= max)
        max = arr[0]
}
return max;

Note the little C trick here: ++arr increments arr , as an array reference, to the next element. 请注意此处的C小技巧:++ arr将arr作为数组引用增加到下一个元素。 You may have seen this already as a character pointer and string variable. 您可能已经将其视为字符指针字符串变量。

Well, it seems you're trying to do something that is easier to do with a loop than recursion. 好吧,看来您正在尝试做比循环更容易做的事情。 You can implement getlargest() like this: 您可以像这样实现getlargest()

int getlargest(int arr[]) {
    int max = arr[0]; // This is safer than initializing max with 0. What if arr[0] is the largest element and all elements are below 0?
    for (int i = 0; i < size; ++i)
        if (arr[i] > max)
            max = arr[i];
    return max;
}

I am not experienced with the concept of recursive functions. 我对递归函数的概念没有经验。

Assuming you want to learn how to use recursion, you should take a look at factorial . 假设您想学习如何使用递归,则应该看一下factorial It's a no-brainer to find the factorial of an integer i using a recursive function. 使用递归函数查找整数i的阶乘是不费吹灰之力的。

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

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