Compute the mean float mean with accumulate function
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <vector>
using namespace std;
template <class T>
void print(T& c){
for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
std::cout << *i << endl;
}
}
int main( )
{
const float a[] = { 1, 1.3, 1.5, 0.9, 0.1, 0.2};
// create and initialize vector with above data
vector<float> data( a,a + sizeof( a ) / sizeof( a[0] ) );
cout << "DATA VECTOR HAS " << data.size() << " ELEMENTS\n";
print( data );
// compute the mean
float mean = accumulate( data.begin(), data.end(), 0.0f )/ data.size();
}
Related examples in the same category