For a homework assignment I have to create a templatized standard deviation function that can be performed on any container. Here's what I have:
template <typename Container>
double findMean(Container c, int count){
double sum = 0;
for (auto&& e : c){
sum += e;
}
sum /= count;
return sum;
}
template <typename Container>
double findStDev(Container c){
double mean = findMean(c, c.size());
std::cout << mean << std::endl;
for (auto&& e : c){
e -= mean;
e *= e;
}
mean = sqrt(findMean(c, c.size()));
return mean;
}
The first time I find the mean I want to divide by the full size of the container (n), but when I find it the second time for the standard deviation, I need to divide by size-1 (n-1).
Is the .size() function available for all c++ containers?