안녕하세요 스택 교환 전문가,부스트 : 벡터로 분포에 포인터 저장
나는 하나의 벡터에서 부스트에 의해 제공되는 다른 통계적 분포에 대한 포인터를 수집하려고합니다. 분포가 하나의 (가상) 부모 클래스에서 파생 될 수 경우에 나는
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);
처럼 뭔가를 작성하고 다음 벡터를 반복하고 간접 참조 포인터로 등의 기능을 적용 할 수 있습니다. 하지만이 경우가 아니기 때문에 포인터를 한 곳에 저장하는 방법을 모릅니다.
그럼 내 질문은, 하나의 변수/목록/벡터 ... (예 : std :: vector처럼 편리하게 처리 할 수있는) 다른 템플릿 클래스에 대한 포인터를 저장하는 방법이 있다면 내 질문입니다.
예를 들어 부스트 pdf 밀도 함수는 특정 유형에 관계없이 참조 해제 된 포인터에 적용될 수 있습니다 (따라서 벡터를 하나의 벡터에 저장하는 것이 의미가 있음).
///////////////////////////////////////////////////////////////////////////// /////////////////////
다른 (좋은) 답변으로 놀고 마침내 부스트를 고수하기로 결정했습니다. : boost :: static_visitor와 결합 된 변형입니다. 아래 은 내 원래의 질문에 설명 된 것을 수행하는 전체 응용 프로그램입니다 :
#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;
}
내가 볼 수있는 유일한 단점은 분포의 유형 (변형에) 명시 적으로 지정 될 필요가 그래서이 향상 될 수 있다는 것입니다 :: any는 더 많은 자유를 부여합니다.
큰 도움을 가져 주셔서 감사합니다.
행크
더 정교한 예제 링크 추가 – sehe