提高:将指针存储在矢量分布 [英] Boost: Store Pointers to Distributions in Vector

查看:132
本文介绍了提高:将指针存储在矢量分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜栈专家交流,

我想收集的指针由升压在一个载体中提供不同的统计分布。
如果发行会从一个(虚拟)父类派生我可以写类似

I'm trying to collect pointers to different statistical distributions provided by Boost in one vector. If distributions would be derived from one (virtual) parent class I could write something like

std::vector<Parent> v;

boost::math::normal_distribution<double> n;
boost::math::students_t_distribution<float> t(4);

boost::math::normal_distribution<double> *p1 = new boost::math::normal_distribution<double>(n);
boost::math::students_t_distribution<float> *p2 = new boost::math::students_t_distribution<float>(t);
v.push_back(p1);
v.push_back(p2);

,然后遍历载体和应用功能等方面的取消引用的指针。
但由于这是不是这样我真的不知道如何指针存储在一个地方?

and then iterate over the vector and apply functions etc. to the dereferenced pointers. But since this is not the case I don't really know how to store the pointers in one place?

我的问题是,如果有存储指针不同的模板类的一个变量/列表/载体的一种方式......(可以方便地处理类似的std ::矢量为例)。

So my question is, if there is a way to store pointers to different template classes in one variable/list/vector... (that can be handled conveniently like std::vector for example).

备注使得例如升压PDF密度函数可以将特定类型的施加到取消引用指针而不管(以便将它们存储在一个矢量有意义在某些情况下)。

Remark that for example the Boost pdf density function can be applied to the dereferenced pointers regardless of the specific type (so storing them in one vector makes sense in some cases).

/////////////////////////////////////////////// ///////////////////////////

//////////////////////////////////////////////////////////////////////////

我的不同(NICE)扮演的答案和周围终于决定要提振::变异与升压:: static_visitor结合。
下面是做什么,我在我原来的问题提出了一个完整的应用程序:

I played around with the different (nice) answers and finally decided to stick to boost::variant in conjunction with boost::static_visitor. Below is a full application that does what I outlined in my original question:

#include <boost/math/distributions.hpp>
#include <boost/variant.hpp>
#include <vector>
#include <iostream>

//template based visitor to invoke the cdf function on the distribution
class cdf_visitor_generic : public boost::static_visitor<double>
{
    public:

        //constructor to handle input arguments 
    cdf_visitor_generic(const double &x) : _x(x) {}

    template <typename T>
    double operator()(T &operand) const {
    return(boost::math::cdf(operand,_x));
    }

    private:
    double _x;
};

//shorten typing
typedef boost::variant< boost::math::normal_distribution<double>, boost::math::students_t_distribution<double> > Distribution;

int main (int, char*[])
{
//example distributions
boost::math::normal_distribution<double> s;
boost::math::students_t_distribution<double> t(1);

//build a variant
 Distribution v = t;

//example value for evaluation
double x = 1.96;

//evaluation at one point
double y = boost::apply_visitor( cdf_visitor_generic(x),v);
std::cout << y << std::endl;


//build a vector and apply to all elements of it:
std::vector<Distribution> vec_v;

vec_v.push_back(s);
vec_v.push_back(t);


for (std::vector<Distribution>::const_iterator iter = vec_v.begin(); iter != vec_v.end(); ++iter){

    //apply cdf to dereferenced iterator
    double test = boost::apply_visitor( cdf_visitor_generic(x), *iter);
    std::cout << test << std::endl;
}
return 0;
}

我看到的唯一的缺点是需要分布类型明确指定(在变种),所以它可能是的boost ::任何增加了更多的自由。

The only drawback I see is that the type of distribution needs to be explicitly specified (in the variant) so it could be that boost::any adds more freedom.

感谢您的帮助很大!

汉克

推荐答案

您可以使用变量

std::vector<boost::variant<
    boost::math::normal_distribution<double>,
    boost::math::students_t_distribution<float>
> > v;

boost::math::normal_distribution<double> n;
boost::math::students_t_distribution<float> t(4);

v.push_back(n);
v.push_back(t);

我有几个答案,说明如何使用这些元素多态(虽然多态性是静态编译typeswitching,而不是虚函数表派遣)。我会尽快添加一个或两个链接。

I have several answers that show how to use these elements "polymorphically" (though the polymorphism is by statically compile typeswitching, instead of vtable dispatch). I'll add a link or two soon.

  • Generating an interface without virtual functions?
  • Avoid RTTI when dealing with pairs of objects
  • More involved: boost::mpl::fold for double parameter abstraction

有些链接的答案展现手工的方式来输入删除

Some of the linked answers show the "manual" approach to type erasure

<子> PS。我也许应该提的boost ::任何过,但我不喜欢有几个原因。我不推荐它用于这一目的。

PS. I should probably mention boost::any too, but I dislike it for several reasons. I shan't recommend it for this purpose.

这篇关于提高:将指针存储在矢量分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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