3

我正在关注一些线程(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));

因此,检索价值似乎存在问题。我怎样才能解决这个问题?

我正在使用提升 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+=该标题中的定义并对其进行了调整。

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

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

4

0 回答 0