gpt4 book ai didi

c++ - 在终止条件下有或没有 return 语句的递归

转载 作者:行者123 更新时间:2023-11-28 07:04:11 29 4
gpt4 key购买 nike

请解释 return 语句如何对 trie 进行简单的递归解析

案例 A:

if (true) push &stack; //push path result onto a stack
else{
if (terminating condition true) return;
else {
condition 1 recursion to next node
condition 2 recursions to next node
...
condition n recursion to next node
}
recursion to next path;
}

案例 B:

if (true) {
push &stack; //push path result onto a stack
return;
}else{
if (terminating condition true) return;
else{
condition 1 recursion to next node
condition 2 recursion to next node
...
condition n recursion to next node
}
recursion to next path;
}

案例 A 对我来说工作正常。但是,我不明白将结果插入堆栈后会发生什么。 “它”如何知道终止这些路径?

最佳答案

对于返回类型为 void 的函数,return 语句不是绝对必要的。如果在没有遇到 return 语句的情况下到达了这样一个函数的末尾,则控制权将传递给调用者,就好像遇到了没有表达式的 return 语句一样。换句话说,隐式返回在最终语句完成时发生,并且控制权自动返回到调用函数。无论如何,你不需要为另一行代码付费,添加一个 return 语句是一个很好的做法。
但请注意,对于使用非 void 返回类型声明的函数,它是必需的。它有时可以在没有 return 语句的情况下工作,例如,这个:Confused about the function return value .但这是未定义行为的结果。

关于c++ - 在终止条件下有或没有 return 语句的递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22001927/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com