嵌套循环在重新函数内未正常运行
Nested loop not running correctly inside recruisive function
嵌套循环的外循环在重新函数内未正常运行。简化示例:
uint max =3;
void func(uint a,uint b,uint depth){
for(; a<max; a++){ // problem here after first recrusion , counting but not going inside braces with recruisive function
for(; b<max; b++){ // inner loop works in recruisive function well
if(depth<2){
func(0,0,depth+1);
}
}
}
}
使用您最初发布的代码:
除非 max 为 0,否则此代码将因无限递归而导致堆栈溢出:递归中的第一次迭代将立即递归地调用函数,从 a 和 b 再次为 0 开始。
使用修改后的代码:
深度限制了递归,避免了无休止的递归。
但是,在第一次外部迭代之后,b 已达到最大值。对于 a 循环中的所有连续迭代,由于您不重置 b,内部循环会立即停止
相关文章:
- 无法获取菜单选择以运行函数.C++
- 为什么std::async使用同一个线程运行函数
- MINGW - 正确运行函数所需的 cdecl
- 如何在 C 中使用空的 main() 方法运行函数?
- 如何让谷歌测试正常运行。测试总是失败。(它不会编译)
- 运行函数作为 constexpr 和不作为 constexpr
- 如何在全局变量的构造函数之前运行函数
- 在 Cap'n Proto RPC 服务器中定期运行函数
- 为什么当我在 c++ 中运行函数时,我的代码显示数字 53
- CLang:在 std::thread 中运行函数会导致结构创建BAD_ACCESS
- 多线程程序卡在优化模式下,但在 -O0 中正常运行
- 如何使用 TBB 在单个线程中运行函数
- 我在运行函数 GetVolumeInformation() 时得到非常随机的结果
- 如何使用 boost::asio io_service 运行函数异步
- c++:if 语句无法正常运行,并继续执行无效输出
- Qt - 在其他线程上运行函数
- 函数在正常运行时不起作用,但在调试时起作用
- 嵌套循环在重新函数内未正常运行
- c++通过exec函数杀死子进程,但不杀死父进程和正常运行时间
- c#包装到c++函数-运行一次,工作正常.运行两次,程序挂起