问题 使用配对累积的问题


我正在使用双端队列,因此我可以为我的数据生成滚动平均值和差异。我将n和n ^ 2作为一对存储在双端队列中,然后使用累积与我自己的运算符+()。

#include <deque>
#include <numeric>
#include <utility>

template <typename T1, typename T2>
std::pair<T1,T2> operator+(const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs)
{
   return std::pair<T1,T2>(lhs.first + rhs.first, lhs.second + rhs.second);
}

namespace resource 
{
template <typename T>
class rollingStats
{
public:
   rollingStats(unsigned int n, const T& val):
      xs(n, std::pair<T,T>(val, val*val))
   {;}
   ~rollingStats()
   {;}

   T getMean(void) const
   {
      std::pair<T,T> sum = std::accumulate(xs.begin(), xs.end(), std::pair<T,T>((T)0,(T)0));
      return sum.first / xs.size();
   }

   T getVar(void) const
   {
      const unsigned int n = xs.size();

      std::pair<T,T> sum = std::accumulate(xs.begin(), xs.end(), std::pair<T, T > ((T)0,(T)0));

      return ((n * sum.second - sum.first*sum.first) / (n * n));
   }

   void addValue(const T& val)
   {
      xs.pop_front();
      xs.push_back(std::pair<T,T>(val,val*val) );
   }

   const std::deque<std::pair<T,T> >& getXs(void) const {return xs;}
private:
   std::deque<std::pair<T,T> > xs;
};
}

我使用g ++ 4.1.2得到了编译错误,我无法解决。

  [ CC         ]  resource/UnitTest: rollingStats_Test.o 
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_numeric.h: In function ‘_Tp std::accumulate(_InputIterator, _InputIterator, _Tp) [with _InputIterator = std::_Deque_iterator<std::pair<float, float>, const std::pair<float, float>&, const std::pair<float, float>*>, _Tp = std::pair<float, float>]’:
../rollingStats.hpp:45:   instantiated from ‘T resource::rollingStats<T>::getMean() const [with T = float]’
rollingStats_Test.cpp:98:   instantiated from here
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_numeric.h:89: error: no match for ‘operator+’ in ‘__init + __first.std::_Deque_iterator<_Tp, _Ref, _Ptr>::operator* [with _Tp = std::pair<float, float>, _Ref = const std::pair<float, float>&, _Ptr = const std::pair<float, float>*]()’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_bvector.h:267: note: candidates are: std::_Bit_iterator std::operator+(ptrdiff_t, const std::_Bit_iterator&)
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_bvector.h:353: note:                 std::_Bit_const_iterator std::operator+(ptrdiff_t, const std::_Bit_const_iterator&)
make: *** [rollingStats_Test.o] Error 1

这里有什么问题?我是否需要添加自己的仿函数而不是仅依靠STL?

谢谢


8473
2018-05-13 16:30


起源

一世 认为 你应该尝试将对的+运算符放入std命名空间;看起来你可能会遇到C ++命名空间解析规则。 - Oliver Seiler
@Oliver:除了技术上不允许你这样做。 - Alexandre C.
@jkp:您从未实例化导致问题的函数。 - Lightness Races in Orbit
我可以看到三个选项:使用带有二进制函数的accumulate形式,使用你需要编写的add_pair函数(可能是最简单的选项);子类std :: pair并给它添加运算符(感觉很脏);添加一个新结构/类,它具有一对或只有您需要的成员,并使用它而不是该对(可能是最灵活的选项)。 - Oliver Seiler
还有另外一个选择(好吧,它只是Oliver Seiler第一个选项的另一个版本) - 移动你的 operator + 到资源名称空间,并将其作为函数参数传递给 accumulate : std::pair<T,T> sum = std::accumulate(xs.begin(), xs.end(), std::pair<T,T>((T)0,(T)0), resource::operator+<T,T>); - a1ex07


答案:


std::pair 没有 operator+,你还没有提供方法 std::accumulate 打电话给你的实施 operator+

我会包装你提供的功能 operator+ 在一个算子...

template <typename T1, typename T2> struct pair_sum : public std::binary_function< std::pair<T1,T2>, std::pair<T1,T2>, std::pair<T1,T2> >
{
    std::pair<T1,T2> operator()(const std::pair<T1,T2>& lhs, const std::pair<T1,T2>& rhs)
    {
       return std::pair<T1,T2>(lhs.first + rhs.first, lhs.second + rhs.second);
    }
};

...并通过调用版本来使用它 std::accumulate 这需要4个参数:

std::pair<T,T> sum = std::accumulate(xs.begin(), xs.end(), std::make_pair((T)0,(T)0), pair_sum<T,T>());

12
2018-05-13 16:56



谢谢。正是我需要的。我曾经以为我可以提供自己的操作员+对,并且它会被拾取。 - DanS
这个答案正是我需要的,谢谢你。 - Ravid Goldenberg


引用 奥利弗塞勒评论:

我可以看到三个选项:使用带有二进制函数的accumulate形式,使用你需要编写的add_pair函数(可能是最简单的选项);子类std :: pair并给它添加运算符(感觉很脏);添加一个新结构/类,它具有一对或只有您需要的成员,并使用它而不是该对(可能是最灵活的选项)。

[这是社区维基的答案。随意编辑以添加更正,样本等。]


0
2018-05-13 19:04





你可以借助帮助得到一对 boost::lambda

#include <boost/lambda/bind.hpp>
#include <boost/lambda/construct.hpp>

template<typename T>
void summarize()
{
  typedef std::pair<T, T> pt_t;
  std::deque<pt_t> xs;
  using namespace boost::lambda;

  // fill xs with useful stuff

  pt_t res = std::accumulate(
    xs.begin(), xs.end(), std::make_pair(T(),T()),
    bind( constructor<pt_t>(),
      bind( std::plus<T>(), bind(&pt_t::first,_1), bind(&pt_t::first,_2) ),
      bind( std::plus<T>(), bind(&pt_t::second,_1), bind(&pt_t::second,_2) )
    ) );
}

0