【问题标题】:Counting inside a recursive function在递归函数内计数
【发布时间】:2016-05-19 21:27:56
【问题描述】:
int countLatticePoints(std::vector<int> &point, const double &radius, const int &dimension) {
static int count = 0;    

for(int i = -(static_cast<int>(std::floor(radius))); i <= static_cast<int>(std::floor(radius)); i++) {
    point.push_back(i);

    if(point.size() == dimension){
        if(isPointWithinSphere(point, radius)) count++;
    }else countLatticePoints(point, radius, dimension);

    point.pop_back();
}

return count;
}

我有上面的递归函数,如果条件isPointWithinSphere( ... ) 为真,我想为它增加一些变量。我最初的方法是声明一个名为count 的静态变量,以便它通过每个递归调用来维护计数。好的,当我第一次调用函数countLatticePoints 时这很好用,但是现在如果我再次调用该函数,它会在我第一次调用countLatticePoints 时添加到先前的count 值。我知道这是由于静态限定符的行为方式而发生的。有什么方法可以在我调用第二个调用完成之前将count 变量重置回 0 吗?我当然不想使用全局变量,有没有其他方法可以在这种情况下工作?

【问题讨论】:

  • 您不需要传递原生类型,例如 doubleint 作为 const &amp;。常量引用通常用于传递大对象或类
  • "当我第一次调用函数 countLatticePoints 时,这很好用,但现在如果我再次调用该函数,它会在我第一次调用 countLatticePoints 时添加到先前的计数值。" - 如果这不是你想要的行为,你想要什么?函数第一次从外面调用并返回后,你的计数应该是多少?在第二次调用并返回后,您的计数应该是多少?
  • @user31264 第一次调用后是 19,第二次调用应该是 51 而不是 19 + 51
  • 在这种情况下还有其他方法可以工作吗? 是的,绝对!您不需要递归来创建排列。请参阅我对您之前问题的回答。 stackoverflow.com/a/37326790/224704

标签: c++ c++11 recursion


【解决方案1】:

您不需要任何静态变量或附加参数。这就是你需要的。

int countLatticePoints(std::vector<int> &point, const double &radius, const int &dimension) {
    int count = 0;
    const int iRadius = std::floor(radius);
    for(int i = -iRadius; i <= iRadius; i++) {
        point.push_back(i);
        if(point.size() == dimension){
            if (isPointWithinSphere(point, radius))
                count++;
        } else
            count += countLatticePoints(point, radius, dimension);
        point.pop_back();
    }

    return count;
}

附带说明,您不需要传递 int 或 double 作为参考。

【讨论】:

    【解决方案2】:

    传递count作为参数;给它一个默认值 0,所以初始调用不必提供它。这能满足您的需求吗?

    【讨论】:

    • 在您的情况下,它在第一次调用时返回 0,我已经尝试过了。
    • 当你尝试这个时,你是否通过引用传递计数?
    • @0x5453 我正在使用这个int countLatticePoints(std::vector&lt;int&gt; &amp;point, const double &amp;radius, const int &amp;dimension, int count = 0)
    • 如果你想在函数之外修改值,应该通过引用传递计数,即int countLatticePoints(std::vector&lt;int&gt; &amp;point, const double &amp;radius, const int &amp;dimension, int&amp; count)。在这种情况下,你不能给它一个默认值;只需确保在第一次调用该函数时将计数设置为零。或者,您可以在从递归调用返回时设置 count 的值,即}else count = countLatticePoints(point, radius, dimension);
    • @0x5453 通过引用传递计数的问题是,如果我再次调用该函数,它会添加到计数的当前值。因此,如果在第一次通话计数为 19 之后,第二次通话将从 19 开始计数
    【解决方案3】:

    您可以使用辅助函数来完成您想要的操作,如下所示:

    void countLatticePoints(std::vector<int> &point, const double &radius, const int &dimension, int& count) {
        const int int_radius = static_cast<int>(std::floor(radius));
        for(int i = -int_radius; i <= int_radius; i++) {
            point.push_back(i);
    
            if(point.size() == dimension){
                if(isPointWithinSphere(point, radius)) count++;
            }else countLatticePoints(point, radius, dimension, count);
    
            point.pop_back();
        }
    }
    
    int countLatticePoints(std::vector<int> &point, const double &radius, const int &dimension) {
        int count = 0;    
        countLatticePoints(point, radius, dimension, count);
        return count;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-04
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 2015-09-21
      • 1970-01-01
      相关资源
      最近更新 更多