简体   繁体   English

递归函数错误

[英]Recursive Function Error

Im trying to create a recursive function that contains a vector of numbers and has a key, which is the number we are looking for in the vector. 我试图创建一个包含数字向量并具有键的递归函数,这是我们在向量中寻找的数字。

Each time the key is found the function should display a count for how many times the key appears in the vector. 每次找到该键时,该功能应显示一个计数,以显示该键在矢量中出现的次数。

For some reason my recursive function is only returning the number 1 (disregard the 10 I was just testing something) 由于某种原因,我的递归函数仅返回数字1(忽略了我刚刚测试的10)

Here's my code: 这是我的代码:

int recursive_count(const vector<int>& vec, int key, size_t start){
    if (start == vec.size())
        return true;
    return (vec[start] == key? 23 : key)
        && recursive_count(vec, key, (start+1));
}


int main() {

    vector <int> coco;

    for (int i = 0; i<10; i++) {
        coco.push_back(i);
    }

    cout << coco.size() << endl;

    int j = 6;


    cout << recursive_count(coco, j, 0) << endl;

}

Not sure what you are trying to do, but as is - your function will return false (0) if and only if the input key is 0 and it is in the vector. 不确定要执行的操作,但实际上-仅当输入key为0并且在向量中时,函数才会返回false(0)。 Otherwise it will return 1. 否则将返回1。

This is because you are basically doing boolean AND operation. 这是因为您基本上是在执行布尔AND运算。 The operands are true for all values that are not 0, and the only way to get a 0 - is if it is in the vector - and the key is 0. 对于所有不为0的值,操作数均为true ,获得0的唯一方法-如果它在向量中-且键为0。

So, unless you get a false (0) along the way, the answer to the boolean formula is true , which provides the 1. 因此,除非您一路走到false (0),否则布尔公式的答案为true ,即为1。


EDIT: 编辑:

If you are trying to do count how many times the key is in vec - do the same thing you did in iterative approach: 如果您要计算keyvec次数,请执行与迭代方法相同的操作:

  1. Start from 0 (make stop condition return 0; instead of return true; ) 从0开始(使停止条件return 0;而不是return true;
  2. Increase by 1 whenever the key is found instead of using operator&& , use the operator+ . 只要找到密钥,就增加1,而不要使用operator&& ,而要使用operator+

(I did not give a direct full answer because it seems like HW, try to follow these hints, and ask if you have more questions). (我没有给出完整的直接答案,因为它看起来像硬件,请尝试遵循这些提示并询问您是否还有其他问题)。

Your recursive_count function always evaluates to a bool 您的recursive_count函数始终计算为bool

You are either explicitly returning true 您要么显式返回true

if (start == vec.size())
  return true;

or returning a boolean compare 或返回布尔比较

return (vec[start] == key? 23 : key) // this term gets evaluated
        &&  // the term above and below get 'anded', which returns true or false.
        recursive_count(vec, key, (start+1)) // this term gets evaluated

It then gets cast to your return type ( int ), meaning you will only ever get 0 or 1 returned. 然后将其强制转换为您的返回类型(int),这意味着您只会返回0或1。

To me it seems that a recursive function for that is nonsense, but anyway... 对我来说,似乎递归函数是无稽之谈,但无论如何...

Think about the recursion concepts. 考虑一下递归概念。

What is the break condition? 中断条件是什么? That the current character being checked is not in the string anymore. 当前正在检查的字符不在字符串中。 You got that right. 你说对了。

But the recursion case is wrong. 但是递归的情况是错误的。 You return some kind of bool (what's with the 23 by the way? The one recursion round needs to return 1 if the current element equals key, and 0 otherwise. 您返回某种布尔值(顺便说一句,用23表示什么?如果当前元素等于key,则一轮递归需要返回1,否则返回0。

Then we only need to add up the recursion results, and we're there! 然后,我们只需要累加递归结果,就可以了!

Here's the code 这是代码

int recursive_count(const vector<int>& vec, int key, size_t start) {
    if (start >= vec.size()) {
        return 0;
    } else {
        return
        ((vec[start] == key) ? 1 : 0) + 
                recursive_count(vec, key, start+1);
    }
}

Since this is even tail-recursion , good compilers will remove the recursion for you by the way, and turn it into its iterative counterpart... 由于这甚至是尾递归 ,因此好的编译器会顺便为您删除递归,并将其变成迭代的对应对象...

As per integral promotion rules on cppreference.com 根据cppreference.com上的integral promotion规则

The type bool can be converted to int with the value false becoming ​0​ and true becoming 1. 布尔类型可以转换为int,值false变为``0'',true变为1。

With, 与,

if (start == vec.size())
        return true;

your function with return type int returns 1 您的返回类型为int函数返回1

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

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