title: Why this can course an out of range exception
body: <p>Why this code runs, VC++ will show a out of range exception? </p>
  
  <p>error message:
  vector Line:933
  Expression: "Standard C++ Libraries Out of Range" &amp; &amp; 0</p>
  
  <p>high is a function to return the highest element in an iterator. Then I construct an array and a vector, use high to find the highest element in them.</p>
  
  <p>This is iterator.h:</p>
  
  <pre><code>template&lt;class Iterator&gt; Iterator high(Iterator first, Iterator last)
  {
      Iterator high = first;
      for(Iterator p = first; p != last; ++p)
          if(*high &lt; *p) high = p;
      return high;
  }
  </code></pre>
  
  <p>This is main function:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include "iterator.h"
  
  using namespace std;
  
  double* get_from_jack(int* count)
  {
      double* p = new double[5];
      p[0] = 2.3;
      p[1] = 3.1;
      p[2] = 2.1;
      p[3] = 1.2;
      p[4] = 4.3;
      *count = 5;
      return p;
  }
  
  vector&lt;double&gt;* get_from_jill()
  {
      vector&lt;double&gt; v;
      v.push_back(2.1);
      v.push_back(3.8);
      v.push_back(5.1);
      v.push_back(2.2);
      v.push_back(1.9);
      v.push_back(4.4);
      vector&lt;double&gt;* p = &amp;v;
      return p;
  }
  
  void fct()
  {
      int jack_count = 0;
      double* jack_data = get_from_jack(&amp;jack_count);
      vector&lt;double&gt;* jill_data = get_from_jill();
  
      double* jack_high = high(jack_data, jack_data+jack_count);
      vector&lt;double&gt;&amp; v = *jill_data;
      double* jill_high = high( &amp;v[0], &amp;v[0]+v.size() );
  
      cout &lt;&lt; "Jill's high " &lt;&lt; *jill_high &lt;&lt; "; Jack's high " &lt;&lt; *jack_high;
  
      delete[] jack_data;
      delete jill_data;
      //delete jack_high;
      //delete jill_high;
  }
  
  int main()
  {
      try{
          fct();
          int n;
          cin &gt;&gt; n;
          return 0;
      }
      catch(exception&amp;e)
      {
          cerr &lt;&lt; e.what();
          return 1;
      }
      catch(...)
      {
          return 2;
      }
  }
  </code></pre>