1

我正在练习 ACM 问题以成为一名更好的程序员,但我对 c++ 还很陌生,而且我在解释我正在阅读的一些法官代码时遇到了麻烦。一堂课的开头是

public:
   State(int n) : _n(n), _p(2*n+1)
   {

然后它被初始化为

State s(n);
s(0,0) = 1;

我正在尝试阅读代码,但我无法理解。State 类似乎只传递了 1 个参数,但程序员在初始化时传递了 2 个参数。另外,究竟是什么设置= 1?据我所知,= 运算符并没有被重载,但以防万一我错过了一些东西,我在下面包含了完整的代码。

任何帮助将不胜感激。

提前致谢

    /*
    * D - Maximum Random Walk solution
     * ICPC 2012 Greater NY Regional
     * Solution by Adam Florence
     * Problem by Adam Florence
     */

    #include <cstdio> // for printf
    #include <cstdlib> // for exit
    #include <algorithm> // for max
    #include <iostream>
    #include <vector>

    using namespace std;

    class State
    {
    public:
       State(int n) : _n(n), _p(2*n+1)
          {
          if (n < 1)
             {
             cout << "Ctor error, n = " << n << endl;
             exit(1);
             }
          for (int i = -n; i <= n; ++i)
             _p.at(i+_n) = vector<double>(n+1, 0.0);
          }

       void zero(const int n)
          {
          for (int i = -n; i < n; ++i)
             for (int m = 0; m <= n; ++m)
                _p[i+_n][m] = 0;
          }

       double operator()(int i, int m) const
          {
    #ifdef DEBUG
          if ((i < -_n) || (i > _n))
             {
             cout << "Out of range error, i = " << i << ", n = " << _n << endl;
             exit(1);
             }
          if ((m < 0) || (m > _n))
             {
             cout << "Out of range error, m = " << m << ", n = " << _n << endl;
             exit(1);
             }
    #endif
          return _p[i+_n][m];
          }

       double& operator()(int i, int m)
          {
    #ifdef DEBUG
          if ((i < -_n) || (i > _n))
             {
             cout << "Out of range error, i = " << i << ", n = " << _n << endl;
             exit(1);
             }
          if ((m < 0) || (m > _n))
             {
             cout << "Out of range error, m = " << m << ", n = " << _n << endl;
             exit(1);
             }
    #endif
          return _p[i+_n][m];
          }

       static int min(int x, int y)
       {
           return(x < y ? x : y);
       }
       static int max(int x, int y)
       {
           return(x > y ? x : y);
       }

    private:
       int _n;

       // First index is the current position, from -n to n.
       // Second index is the maximum position so far, from 0 to n.
       // Value is probability.
       vector< vector<double> > _p;
    };

    void go(int ds)
       {
       // Read n, l, r
       int n, nds;
       double l, r;
       cin >> nds >> n >> l >> r;
       const double c = 1 - l - r;

       if(nds != ds){
           cout << "Dataset number " << nds << " does not match " << ds << endl;
           return;
       }

       // Initialize state, probability 1 at (0,0)
       State s(n);
       s(0,0) = 1;

       State t(n);

       State* p1 = &s;
       State* p2 = &t;

       for (int k = 1; k <= n; ++k)
          {
          // Compute probabilities at step k

          p2->zero(k);

          // At step k, the farthest from the origin you can be is k
          for (int i = -k; i <= k; ++i)
             {
              const int mm = State::min( State::max(0, i+k), k);
             for (int m = 0; m <= mm; ++m)
                {
                // At step k-1, p = probability of (i,m)
                const double p = p1->operator()(i,m);
                if (p > 0)
                   {
                   // Step left
                   p2->operator()(i-1, m) += p*l;
                   // Step right
                   p2->operator()(i+1, State::max(i+1,m)) += p*r;
                   // Stay put
                   p2->operator()(i, m) += p*c;
                   }
                }
             }
          swap(p1, p2);
          }

       // Compute expected maximum position
       double p = 0;
       for (int i = -n; i <= n; ++i)
          for (int m = 0; m <= n; ++m)
             p += m * p1->operator()(i,m);

       printf("%d %0.4f\n", ds, p);
       }

    int main(int argc, char* argv[])
       {
       // Read number of data sets to process
       int num;
       cin >> num;

       // Process each data set identically
       for (int i = 1; i <= num; ++i)
          go(i);

       // We're done
       return 0;
       }
4

4 回答 4

6

您将调用state::operator()(int, int)与初始化混淆了。该运算符调用允许您设置类实例的元素的值。

State s(n);  // this is the only initialization
s(0,0) = 1;  // this calls operator()(int, int) on instance s
于 2013-03-12T14:14:10.343 回答
0

在这一行:

s(0,0) = 1;

它叫这个:

 double& operator()(int i, int m)

并且因为它返回对双精度的引用,所以您可以分配给它。

于 2013-03-12T14:15:16.587 回答
0

第二行不再初始化。构造函数在第 1 行调用,第二行调用

双&运算符()(int i,int m)

n=0 和 m=0 并将 1 写入返回的引用。

于 2013-03-12T14:18:03.227 回答
0

这部分:

State(int n) : _n(n), _p(2*n+1)

...是一个成员初始化列表。这有点类似于如果你写了这样的结构:

state(int n) { _n = n; _p = 2*n+1; }

...除了它初始化_n_p不是从它们初始化开始,然后为它们分配值。在这种特定情况下,可能没有太大区别,但是当您拥有只能初始化(未分配)的引用之类的东西时,它变得至关重要。

s(0,0) = 1外观s旨在表现得有点像二维数组,并且它们已经重载以operator()充当该数组的下标运算符。我在上一个答案中发布了一个可以做到这一点的课程。

于 2013-03-12T14:20:01.623 回答