boost :: accumulator:std :: vector不适用于rolling_mean

时间:2014-09-03 09:56:30

标签: c++ boost vector boost-accumulators

我关注某个帖子(thisthisthis),以便使用std :: vector作为样本来计算滚动均值。

我已经包含了numeric::functional子图书馆的标题,以便使用向量

此代码无法编译

#include <cstdlib>
#include <boost/accumulators/numeric/functional/vector.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>
#include <boost/unordered_map.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/assign/list_of.hpp>

using namespace std;
using namespace boost::accumulators;

typedef accumulator_set<vector<float>, tag::rolling_mean> RollingMeanAccumulator;
typedef boost::shared_ptr<RollingMeanAccumulator> RollingMeanAccumulatorPtr;

int main(int argc, char** argv) {

    RollingMeanAccumulatorPtr ptr = RollingMeanAccumulatorPtr(new RollingMeanAccumulator(vector<float>(2), tag::rolling_window::window_size = 4));
    const vector<float> v = boost::assign::list_of(2.0)(3.0);
    (*ptr)(v);
    rolling_mean(*(ptr));

    return 0;
}

如果我评论这一行,它会编译

rolling_mean(*(ptr));

因此,检索价值似乎存在问题。我该如何解决这个问题?

我正在使用boost 1.48。

编辑:

@doctorlove在编译错误中注意到operator-=错过了std::Vector。我已经打开boost/accumulators/numeric/functional/vector.hpp并且我已经看到此操作符的定义未命中。所以我已经在我的代码中添加了它以这种方式更改它(抱歉,如果它写得不好):

#include <cstdlib>
#include <boost/accumulators/numeric/functional/vector.hpp>

namespace boost { namespace numeric { namespace operators {
template<typename Left>
    std::vector<Left> &
    operator -=(std::vector<Left> &left, std::vector<Left> const &right)
    {
        BOOST_ASSERT(left.size() == right.size());
        for(std::size_t i = 0, size = left.size(); i != size; ++i)
        {
            numeric::minus_assign(left[i], right[i]);
        }
        return left;
    }
}}}


#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>
#include <boost/unordered_map.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/assign/list_of.hpp>

using namespace std;
using namespace boost::accumulators;

typedef accumulator_set<vector<float>, tag::rolling_mean> RollingMeanAccumulator;
typedef boost::shared_ptr<RollingMeanAccumulator> RollingMeanAccumulatorPtr;

int main(int argc, char** argv) {

    RollingMeanAccumulatorPtr ptr = RollingMeanAccumulatorPtr(new RollingMeanAccumulator(vector<float>(2), tag::rolling_window::window_size = 4));
    const vector<float> v = boost::assign::list_of(2.0)(3.0);
    (*ptr)(v);
    rolling_mean(*(ptr));

    return 0;
}

我在该标题中采用了operator+=的定义并对其进行了调整。

我这样做是因为在this post中,用户说必须在累加器定义之前定义向量运算符。

但即使在这种情况下,我也无法编译它。

0 个答案:

没有答案
相关问题