Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4626790
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:28:19+00:00 2026-05-22T03:28:19+00:00

I am using a deque so I can generate rolling averages and variances for

  • 0

I am using a deque so I can generate rolling averages and variances for my data. I store n and n^2 as a pair in the deque and then use accumulate with my own operator+().

#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;
};
}

I get a compilation error using g++ 4.1.2 which I can’t resolve.

  [ 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

What have I got wrong here? Do I need to add my own functor instead of relying on the STL alone?

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:28:20+00:00Added an answer on May 22, 2026 at 3:28 am

    std::pair doesn’t have an operator+, and you haven’t provided a way for std::accumulate to call your implementation of operator+.

    I would wrap the functionality you provided in operator+ in a functor…

    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);
        }
    };
    

    …and use that by calling the version of std::accumulate that takes 4 arguments:

    std::pair<T,T> sum = std::accumulate(xs.begin(), xs.end(), std::make_pair((T)0,(T)0), pair_sum<T,T>());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can be realized the auto keyword functionality without using c++0x standard? for(std::deque<std::pair<int, int>
We can initialize a container deque by using standard input like this: deque<int> c((istream_iterator<int>(cin)),(istream_iterator<int>()));
What is the best way to store dynamic data for use in VBO (or
I am writing a compiler and use deque to store methods labels of a
I'm using a std::deque to store a fairly large number of objects. If I
I've changed some code that used a list to using a deque. I can
I'm getting a weird error, when initializing my deque or vector. I'm using QtCreator
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the
I am currently creating my deque object using the following, self.CommandList = deque((['S', False,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.