Use accumulate to calculate the sum and use the mod operator to maintain the circular buffer. Not sure on the performance of accumulate though.

```
class MovingAverage {
private:
vector<int> average;
int curp;
int size;
public:
/** Initialize your data structure here. */
MovingAverage(int size) : curp(0), size(size) {
}
double next(int val) {
if (average.size() == size)
{
curp = curp == size ? 0 : curp;
average[curp % size] = val; ++curp;
return (accumulate(average.begin(), average.end(), 0.0)) / size;
}
else
{
average.push_back(val);
return (accumulate(average.begin(), average.end(), 0.0)) / (double)average.size();
}
}
};
```