Hi all,

I have a simple mathematical series that I need to expand fully and I am not sure what the most efficient way is of achieving this. I know I can write a for-loop quite simply to do this but I don't know if a solution already exists in e.g., STL.

I discovered that I can sum elements of a vector using accumulate(); and thought that maybe somehow I can prepare the elements of the vector before they are subjected to the function. Basically, my series is quite simple, I have a constant exponent value and I have to raise the value of each of the vector elements by this exponent, and then sum all the elements of the array.

The big issue I'm having right now though is that the vector I am using is a vector of object pointers, and the values to be summed in the series belong to a member variable of those objects.

I want to code this series expansion into one of my class functions (another class from the class that produces the objects I'm using).

I've tried to do the following, but it doesn't seem to be working:

void FooClass::expandSeries()
{
    sum = accumulate(pointerVector->value.begin(),pointerVector->value.end(),0);
}

The error is get is:

simulation.cpp:30: error: base operand of '->' has non-pointer type 'std::vector<ObjectName*, std::allocator<ObjectName*> >'

I don't understand this as the vector is clearing a vector of pointers and it works like that when I'm using it in other parts of the code.

I would appreciate if someone could help me sort out how to do this with accumulate (and also then how best to raise all of the vector elements to the exponent value before submitting them to the function). If not with accumulate, I'd appreciate any advice on how to do this efficiently.

I think it can be done with Boost but if there's some way of avoiding that, I'd like to hear so. Otherwise I might just revert to a simple for-loop and deference each object.

Thanks a lot,

Kartik

I would appreciate if someone could help me sort out how to do this with accumulate (and also then how best to raise all of the vector elements to the exponent value before submitting them to the function). If not with accumulate, I'd appreciate any advice on how to do this efficiently.

The trick is implementing the function you pass to accumulate. Here is a demonstration that uses a vector of int pointers:

#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

template <class T>
T adder( T init, T* iter ){ return init + *iter; }

int main(){
    int n = 1000;
    vector<int*> values( n, NULL );
    for( int i=0; i<n; i++ )
        values[i] = new int(i);
    int init = 0;
    int result = accumulate( values.begin(), values.end(), init, adder<int> );
    cout << result << endl;
    return 0;
}

The trick is that the adder function gets the init value as its first argument. The second argument is the iterator for the element to be accumulated next. In our case, this is a pointer, so we need to dereference it. I hope this helps and is the sort of solution you were looking for. Note that you could make adder a static function belonging to a class of type T.

Comments
Clear example
This article has been dead for over six months. Start a new discussion instead.