Listing 2: A generic sample standard deviation algorithm, Version 0.8

template<typename _It>
double SampleStandardDeviation(_It it1, _It it2)
{
  // Calculate the length of the sequence and the sum of
  // the values.
  //
  int nNumPoints = 0;
  double dblSum = 0.0;
  double dblSsq = 0.0;
  while( it1 != it2 )
  {
    dblSum += *it1;
    dblSsq += *it1 * *it1;
    ++nNumPoints;
    ++it1;
  }

  // Return the sample standard deviation.
  double n = static_cast<double>(nNumPoints);
  return nNumPoints <= 1 ? 0.0 : (dblSsq  - dblSum * dblSum / n) /
  (n - 1.0) ;
}
— End of Listing —