Pregunta

I'm using boost accumulators to get statistics from a vector of doubles in C++. Each time I need a new statistic, I'm calling a custom method that gets it by creating the accumulator, loading it with the vector values and lastly using the boost statistic function eg.

double Entity::min(void) {
    accumulator_set< double, features< tag::min > > acc;
    // Now load the vector into the accumulator.
    acc = std::for_each( samples.begin(), samples.end(), acc );
    // Specify namespace to avoid name clash of this min method
    this->_min =  boost::accumulators::min(acc);
    return this->_min;
}
// etc. more methods for StdDev, mean, max etc.

My question is: does each creation of an accumulator in different methods duplicate (plus) the vector memory requirements? I know I could write a 'getStatstics' method which would get them all at once, but I'm wondering specifically if the creation and loading of an accumulator uses at least as much memory as the original vector each time its made.

Thanks guys

Pete

¿Fue útil?

Solución

The algorithm you are using works on the samples collection, which is an attribute of the class Entity. The collection is an instance of the vector template.

The std::for_each instance works only on iterators of the collection, which are passed at construction time : samples.begin() and samples.end().

Standard iterators are especially designed to abstract the notion of cursor or pointer on a collection. They do not duplicate the collection in any way, so you should not worry about memory allocation issue when using these.

The specific accumulator_set used here also doesn't produce any copy of the collection. It merely access the iterator, does a comparison between the current min candidate and the current pointed value of the collection, and keep the best of both for the next iteration.

As it stands, none of the standard parts you are using duplicates the collection.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top