I have a problem with this std::accumulate function. When compiling this I have quite much compile notifications but it compiles.
One that says:
warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
This is what is happening here. I have declared the elements in vector "vec" to doubles with 2 decimals.
However the output of this will be 15 wich is an integer. The output should have been. (10,50 + 20,50 / 2) = 15,50.
I wonder what I could be doing wrong since everything is declared to doubles. Where is the conversion from double to int that seems to be happening.
vec = 10.50;
vec = 20.50;
double sum = std::accumulate( vec.begin() + 1, vec.begin() + 3, 0);
double Average = ( sum / (3 - 1) );
out << Average
The function template determine the type based on the initial value. 0 is an int literal, 0.0 is a double literal, so if you want to instantiate std::accumulate<double>, you would write:
double sum = std::accumulate(vec.begin() + 1, vec.begin() + 3, 0.0);
Actually, it's not std::accumulate<double>, but effectively std::accumulate<std::vector<double>::iterator, double>.
okay, that was the thing... It worked fine.