跟踪多少次在C ++中调用了一个递归函数 [英] Keep track of how many times a recursive function has been called in C++

查看:48
本文介绍了跟踪多少次在C ++中调用了一个递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个程序,该程序的功能是参数为字符串向量.我想对该函数使用递归,但是每次调用该函数时,我都想更改参数,例如

I am trying to work on a program that has a function whose parameter is an vector of string. I want to use recursive on that function but everytime the function is called, I want to change the parameter to say for example

fun(stringArray[i]) 

其中,i是函数被调用的次数.

where i is the number of time the function has been called.

所以以更简单的方式类似以下内容.但是我需要跟踪功能fun被执行了多少次.

So in simpler way something like following. But I need to keep track of how many times the function fun has been executed.

void fun(){
    cout<<hi;
    if(x!=10)
    fun()
}

int main(){

    fun();
}

在这个例子中,我想只打印10次,所以想要一个递增的变量,当变量达到10时,它将停止.因此,总的来说,我应该怎么做才能跟踪它?我尝试使用全局变量,但它们似乎不适用于函数.有什么建议吗?

In this one let's say I want to print it out just 10 times, so want to have a varible that increments, and when reaches 10, it stops. So in general what can I do to keep track of it? I tried using global variables but they don't seem to work with functions. Any suggestions?

推荐答案

我在这里看到了一片混乱,所以我决定将其清除.

I've seen quite a mess here so I decided to clear the things out.

解决方案0:静态变量

考虑对建议的代码进行少量修改

Consider the code proposed with a small modification

#include<iostream>
using namespace std;

void fun()
{
    static int count=1;
    count++;
    cout << "fun() is called " << count << " times" << endl;
    if(count<=10)
    {
            fun();
    }
}

int main()
{
    cout << "first call" << endl;
    fun();
    cout << "second call" << endl;
    fun();
    cout << "third call" << endl;
    fun();
}

结果如下:

first call
fun() is called 2 times
fun() is called 3 times
fun() is called 4 times
fun() is called 5 times
fun() is called 6 times
fun() is called 7 times
fun() is called 8 times
fun() is called 9 times
fun() is called 10 times
fun() is called 11 times
second call
fun() is called 12 times
third call
fun() is called 13 times

如您所见,使用静态变量可能会导致某些意外行为.

As you can see, using static variables could lead to some unexpected behaviour.

这是一个单发功能,将来会引起您很多头痛.此外,使用静态变量会导致代码不可读,容易出错

This is a one shot function that will cause you quite some headaches in the future. Furthermore, the usage of static variables leads to an unreadable code that is error prone

别这么做!

解决方案1:按值传递变量

考虑以下代码:

#include <iostream>
using namespace std;

void fun(int i){
    cout<<i<<endl;
    if(i!=3) {
        i++;
        fun(i);
        fun(i);
    }
}

int main(){
    fun(0);
}

这是输出:

0
1
2
3
3
2
3
3
1
2
3
3
2
3
3

您可以看到输出不是该函数被调用的次数

As you can see the output is not the number of times the function is called

解决方案2:通过引用传递的变量

#include <iostream>
using namespace std;

void fun(int& x){
    if(x>=10)
        return;
    ++x;
    cout << x << endl;
    fun(x);
}

void funEntry(){
    int x = 0;
    cout << "Entry point" << endl;
    fun(x);
}

int main(){
    funEntry();
    funEntry();
}

将打印

Entry point
1
2
3
4
5
6
7
8
9
10

这种方法也适用于像这样的更奇特的递归模式

This approach will work also with some more exotic recursive pattern like this one

#include <iostream>
using namespace std;

void fun(int i, int& x){
    if(i>=4)
        return;
    ++x;
    cout << i << " " << x << endl;
    fun(i+1,x);
    fun(i+2,x);
}

void funEntry(){
    int x = 0;
    cout << "Entry point" << endl;
    fun(0,x);
}

int main(){
    funEntry();
    funEntry();
}

输出:

Entry point
0 1
1 2
2 3
3 4
3 5
2 6
3 7
Entry point
0 1
1 2
2 3
3 4
3 5
2 6
3 7

这篇关于跟踪多少次在C ++中调用了一个递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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