如何跟踪递归深度? [英] How do I track recursion depth?

查看:78
本文介绍了如何跟踪递归深度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在解决的递归函数.我想输出递归深度...即该函数调用自身的次数.

I have a recursive function that I am troubleshooting. I want to output the recursion depth...i.e. how many times the function calls itself.

如果我使用的静态变量在函数顶部递增,而在底部递减.

If I use a static variable that increments at the top of the function and decrements at the bottom.

这会准确地给我深度吗?

Will this accurately give me the depth level?

recurse()
  {
  static int depth = 0;
  depth++;
  /*
  do stuff
  recurse()
  */
  depth--;
  return return_value;
  }

推荐答案

recurse(int depth = 0){
    ....
    recurse(depth+1);
}

这篇关于如何跟踪递归深度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆