6

我想做的是:

class A
{
    public:
    double sum(double a, double b);
    double max(double a, double b);
}

template <typename T>
class B
{
    std::vector<T> data;

    public:

    double sum (double a, double b);
    double max (double a, double b);
    double average( MyFunction, double a, dobule b)
    {
        double sum = 0;
        int n = data.size();

        for ( int i = 0; i < n; i++)
            sum = sum + data[i].MyFunction(double a, double b)

            return sum / n;
    }

}

例子:

double average( max, double a, double b)
{
    double sum = 0;
    int n = data.size();

    for ( int i = 0; i < n; i++)
        sum = sum + data[i].max(double a, double b)

        return sum / n;
}

为什么?

  1. 这样可以节省我编写函数的时间:求和的平均值。最大值的平均值 min 的平均值,它们都是非常相似的函数。
  2. 它的编码方式B< B< A> >有效

我尝试了什么?

  • 函数指针
  • S1

      typedef double (A::*MyFunctionf)(double, double);
      typedef double (B<A>::*MyFunctionff)(double, double);
      typedef double (B<B<A> >::*MyFunctionfff)(double, double);
    
    • 有用。问题:
      • 函数指针的3-4 typedef声明不美观
      • 如果我想在 B 中编写发送函数指针的函数,它将被硬编码,并且 3 个 typedef 中只有 1 个可以硬编码。含义:它不适用于所有情况
  • S2(基于模板类型定义 - 你的工作是什么?):

      template <typename rtype, typename t>
      struct CallTemplate
      {
        typedef rtype (t::*ptr)(double, double);
      };
    
     // the function becomes :
     template <typename T>
     template<typename rtype>
     double B<T>::average(CallTemplate<double, T> MyFunction, double a, double b)
     {
        double sum = 0;
       int n = data.size();
    
          for ( int i = 0; i < n; i++)
            sum = sum + (data[i].*MyFunction)( a, b)
    
        return sum / n;
      }
    

    例子:

     // makes an error "Myfunction was not declared" + " 
     // dependent-name'{anonymous}::CallTemplate<double, A>::ptr' 
     // is parsed as a non-type, but instantiation yields a type"
     CallTemplate<double, A>::ptr MyFunction = &A::max; 
     Average(max, t, v);
    

我不知道问题出在哪里。我也尝试过 Boost.Function

4

3 回答 3

2

是的,这是可能的。

您正在寻找成员指针。然而,语法并不明显:

struct A
{
    double x, y;
    A(double x, double y) : x(x), y(y) {}
    double sum() { return x + y; }
    double max() { return std::max(x, y); }
};

这是一个定义了几个方法(summax)的类。

template <typename T>
struct B
{
    std::vector<T> data;
    double average(double (T::*MyMethod)())
    {
        double sum = 0;
        int n = data.size();
        for (int i = 0; i < n; i++)
            sum = sum + (data[i].*MyMethod)();
        return sum / n;
    }
};

这是一个具有接受方法指针的方法的类,它将计算在向量元素上调用指向方法的结果的平均值。

传递这两种方法的一个例子A是:

int main(int argc, const char *argv[]) {
    B<A> b;
    b.data.push_back(A(1, 2));
    b.data.push_back(A(3, 4));
    b.data.push_back(A(5, 6));
    std::cout << b.average(&A::max) << std::endl;
    std::cout << b.average(&A::sum) << std::endl;
    return 0;
}
于 2013-09-22T14:04:40.523 回答
0

Boost.Function 解决方案

class A
{
    public:
    double sum(double a, double b);
    double max(double a, double b);
}

template <typename T>
class B
{
    std::vector<T> data;

    public:

    double sum (double a, double b);
    double max (double a, double b);
    double average(boost::function<double (T*, double, double) MyFunction, double a, dobule b)
    {
        double sum = 0;
        int n = data.size();

        for ( int i = 0; i < n; i++)
            sum = sum + MyFunction(&data[i], a, b)

            return sum / n;
    }

}

例子

boost::function<double (A*, a, b)> MyFunction = &A::max;
average(MyFunction, a, b);

工作示例::

#include <cmath>

using namespace std;

#include <vector>
#include <boost/function.hpp>

#define CallTemplate(returnvalue, FINClass) boost::function<returnvalue (FINClass*, double, double)>

class zc
{
    double n;
    double r;
    double p;

    public:
    zc(double nn, double rr, double pp):n(nn), r(rr), p(pp) {}

    double pv(double t, double v) { return p/ pow ( ( 1 + r + v), ( n - t) );}
    double d(double t, double v)  { return (n - t); }
    double c(double t, double v)  { return (n - t)*(n - t)+(n - t); }
};


template <typename T>
class Master
{
    public:

    Master(){}
    std::vector<T> data;

    double pv(double t, double v) {CallTemplate(double, T) f = &T::pv;  return sum(f, t, v);}
    double d(double t, double v)  {CallTemplate(double, T) f = &T::d;   return weightedAverage(f, t, v);}
    double c(double t, double v)  {CallTemplate(double, T) f = &T::c;   return weightedAverage(f, t, v);}

    double sum(CallTemplate(double, T) MyFunction, double t, double v)
    {
        double sum = 0;
        for( int i = 0, n = data.size(); i < n; i++)
            sum = sum + MyFunction(&data[i], t, v);

        return sum;
    }

    double weightedAverage(CallTemplate(double, T) MyFunction, double t, double v)
    {
        double sum = 0;
        double weight = 0;
        double buf =0;

        for( int i = 0, n = data.size(); i < n; i++)
        {
            buf = data[i].pv(t, v);

            sum = sum + buf;

            weight = weight + MyFunction(&data[i], t, v) * buf;
        }

        return weight/sum;
    }

};

int main()
{
    Master<zc> A;

    for (int i = 1; i < 10; i++)
        A.data.push_back(zc(i, 0.1, 100));

    A.data.push_back(zc(10, 0.1, 1100));


    cout << A.pv(0, 0) << endl;
    cout << A.d(0, 0) << endl;
    cout << A.c(0, 0) << endl;

    return 0;
}
于 2013-09-22T14:08:22.467 回答
0

做这些事情的常见 C++ 习惯用法是传递一个函数“对象”并将其作为模板参数接受:

#include <algorithm>

// definition of class A by user http://stackoverflow.com/users/320726/6502
// in answer http://stackoverflow.com/a/18944672/420683
struct A
{
    double x, y;
    A(double x, double y) : x(x), y(y) {}
    double sum() const { return x + y; }
    double max() const { return std::max(x, y); }
};

template <typename T>
struct B
{
    std::vector<T> data;
    template<class Function>
    double average(Function f)
    {
        double sum = 0;
        int n = data.size();
        for (int i = 0; i < n; i++)
            sum = sum + f(data[i]);
        return sum / n;
    }
};

#include <functional>

int main()
{
    B<A> b{ {{1.0, 42.0}, {2.0, 43.0}, {3.0, 44.0}} };
    b.average([](A const& p){ return p.sum(); });
    b.average(std::mem_fn(&A::sum));
}

这是一种非常通用的方法,因为它不仅接受函数指针或成员函数指针,还接受任何类型的可调用对象。使用成员函数指针很简单,通过std::mem_fn.

于 2013-09-22T14:08:37.530 回答