Boost logo

Boost Users :

Subject: [Boost-users] Troubles using boost::circular_buffer
From: Ted Byers (r.ted.byers_at_[hidden])
Date: 2012-04-12 23:54:41


I declare I use it:

 

#include <boost/circular_buffer.hpp>

 

I make my function:

 

double calculateStdDev(const boost::circular_buffer<double>& input, double&
mean, double& variance, double& stddev) {

  if (input.empty()) return 0.0;

  boost::circular_buffer<double>::const_iterator it = input.begin(), end =
input.end();

  double M = *it;

  double Q = 0.0;

  unsigned int count = 1;

  ++it;

  while (it != end) {

    unsigned int k = count;

    ++count;

    double del = *it - M;

    unsigned int j = k + 1;

    Q += k * (del * del) / j;

    M += del / j;

    ++it;

  }

  variance = Q / (count - 1);

  stddev = std::sqrt(variance);

  mean = M;

  return stddev;

}

 

This function doesn't need to know are care about the number of items in the
buffer. It just makes one pass through the data, from it = inut.begin() to
input.end(). This particular algorithm, BTW, provides a solution that is
MUCH faster and much more accurate in some cases, than the two pass
algorithm, and is much more accurate than the other single pass algorithms
you may see recommended. But that isn't the point.

 

In the header that provides the prototypes for the functions in this
particular CPP file, I have:

 

double calculateStdDev(const boost::circular_buffer<double>& input, double&
mean, double& variance, double& stddev);

 

And gcc v 4.3.4 gives me the following error:

 

../../include/distribution.moments.hpp:9: error: wrong number of template
arguments (1, should be 2)

/usr/local/include/boost/circular_buffer/base.hpp:70: error: provided for
'template<class T, class Alloc> class boost::circular_buffer'

 

I am not sure I understand why.

 

Does this mean that I have to make calculateStdDev a template function?
E.g.:

 

template <typename T>

double calculateStdDev(const boost::circular_buffer<double, T>& input,
double& mean, double& variance, double& stddev) {

                // and put the entire implementation here in the header
instead of in the cpp file with the overloads for other types such as
std::vector)

}

 

But, if that is the case, why is it possible to instantiate such a buffer
using the following (as in the example)?

 

boost::circular_buffer<int> cb(3);

 

 

I must have missed something, but I don't know what.

 

Cheers

 

Ted

 



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net