title: Implementing statistic functions using iterators in C++
body: <p>I need to use basic statistic functions (like mean, variance, quantile) for collections defined using standard iterators (e.g. for subarrays of a <code>boost::multi_array</code>). Are these implemented somewhere in the standard libraries or boost?</p>
  
  <p>Even if there is a ready-made implementation, I found this interesting exercise in templates. Because these methods don't have any parameters with the same type as the result, I tried to define them as follows:</p>
  
  <pre><code>template&lt;class TIter&gt;
  TIter::value_type mean(TIter begin, TIter end)
  {
      int n = 1;
      TIter::value_type result = *begin;
      begin++;
      while(begin!=end)
      {
          n++;
          result+=*begin;
          begin++;
      }
      return result/n;
  }
  </code></pre>
  
  <p>but it obviously doesn't compile:</p>
  
  <pre><code>stats.h:17:1: error: need ‘typename’ before ‘TIter::value_type’ because ‘TIter’ is a dependent scope
  </code></pre>
  
  <p>What would be the best way to define such a function?</p>