简体   繁体   English

如何使用向量参数编写递归 function?

[英]How can I write a recursion function with a vector parameter?

I have a function that takes a vector as a parameter, scan this vector and generates a random word.我有一个 function ,它以一个向量作为参数,扫描这个向量并生成一个随机词。 It's expected from me that the generated words' letters are different from each other.我期望生成的单词的字母彼此不同。 So, I want to check it with a simple if-else condition inside the same function.所以,我想在同一个 function 中用一个简单的 if-else 条件来检查它。 If all letters are different, function returns this word.如果所有字母都不同,则 function 返回此单词。 If not, I need to use the same function which I am already inside while using conditions.如果没有,我需要使用相同的 function 在使用条件时我已经在里面。 But first parameter that I used in the main function doesn't work when I attempt to use it for the second time.但是当我第二次尝试使用它时,我在主 function 中使用的第一个参数不起作用。 Here the generateaRandomWord(vector a) function:这里生成aRandomWord(vector a) function:

vector<string> currentVector;
string generateaRandomWord(vector<string> a) {
    currentVector = a;
    string randomWord;
    int randomNumber = rand() % currentVector.size();
    randomWord = currentVector.at(randomNumber);
    
    if (hasUniqueChars(randomWord)) {
        return randomWord;
    }
    else {
        generateaRandomWord(currentVector);
    }
    
}

I thought that it is a good idea to keep a vector (currentVector) outside of the function.我认为在 function 之外保留一个向量(currentVector)是个好主意。 So, for the first time I use the function this vector will be defined and I will be able to use it if using recursion is necessary.所以,我第一次使用 function 这个向量将被定义,如果需要使用递归,我将能够使用它。 But that didn't work either.但这也没有用。

The main problem you have is that your recursive case doesn't return anything -- it throws away the returned value from the recursive call, then falls off the end of the function (returning garbage -- undefined behvaior).您遇到的主要问题是您的递归案例不返回任何内容——它从递归调用中丢弃返回的值,然后从 function 的末尾脱落(返回垃圾——未定义的行为)。 You need to actually return the value returned by the recursive call:您需要实际返回递归调用返回的值:

        return generateaRandomWord(currentVector);

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

相关问题 如何仅通过在F#中使用递归来编写此函数? - How can I write this function only by using recursion in F#? 如何编写可以接收数组或向量的 function? - How to write a function that can take in an array or a vector? 我如何处理此类函数中的递归? - how can i handle the recursion in this class function? 我如何为用户输入编写内联 function,将其存储在向量中并使用菜单选项调用它? - How can i write a inline function for user input, store it in a vector and call it using menu options? 如何编写一个JavaScript函数,该函数将调用作为参数传递的另一个函数? - How can I write a JavaScript function that will call another function passed as a parameter? 如何编写函数将字符向量转换为分钟? - How do I write function to convert a character vector to minutes? 如何在Golang中编写一个以多种类型为参数的函数? - How can I write a function in Golang which takes multiple types as a parameter? 如果 function 的参数在语句中是字符串,我该如何编写? - How do I write a parameter of a function if its a string in a statement? 如何编写一个接受回调作为参数的jquery函数 - How do I write a jquery function that accepts a callback as a parameter 如何编写此递归Haskell函数 - How can I write this recursive Haskell function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM