title: crash when using upper_bound in C++
body: <p>I have following program which crashed at upper bound call. I am not getting why there is a crash. Any reason why I am having a crash. Thanks for your help and time.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  enum quality { good = 0, bad, uncertain };
  
  struct sValue {
     int time;
     int value;
     int qual;
  };
  
  struct CompareLowerBoundValueAndTime { 
      bool operator()( const sValue&amp; v, int time ) const {
          return v.time &lt; time;
      } 
  
      bool operator()( const sValue&amp; v1, const sValue&amp; v2 ) const { 
          return v1.time &lt; v2.time; 
      }
  
      bool operator()( int time1, int time2 ) const { 
          return time1 &lt; time2; 
      }   
  
      bool operator()( int time, const sValue&amp; v ) const  { 
          return time &lt; v.time;    
      } 
  }; 
  
  struct CompareUpperBoundValueAndTime { 
      bool operator()( const sValue&amp; v, int time ) const {
          return v.time &gt; time;
      } 
  
      bool operator()( const sValue&amp; v1, const sValue&amp; v2 ) const { 
          return v1.time &gt; v2.time; 
      }
  
      bool operator()( int time1, int time2 ) const { 
          return time1 &gt; time2; 
      }   
  
      bool operator()( int time, const sValue&amp; v ) const  { 
          return time &gt; v.time;    
      } 
  }; 
  
  class MyClass {
  
  public:
      MyClass() {
          InsertValues();
      }
  
         void InsertValues();
  
         int GetLocationForTime(int time);
  
         void PrintValueContainer();
  
  private:
  
      vector&lt;sValue&gt; valueContainer;
  };
  
  void MyClass::InsertValues() {
      for(int num = 0; num &lt; 5; num++) {
          sValue temp;
          temp.time = num;
          temp.value = num+1;
          temp.qual = num % 2;
          valueContainer.push_back(temp);
      }
  }
  
  void MyClass::PrintValueContainer()
  {
      for(int i = 0; i &lt; valueContainer.size(); i++) {
          std::cout &lt;&lt; i &lt;&lt; ". " &lt;&lt; valueContainer[i].time &lt;&lt; std::endl;
      }
  }
  
  
  
  
  int MyClass::GetLocationForTime(int time)
  {
      std::vector&lt; sValue &gt;::iterator lower, upper;
      lower =  std::lower_bound(valueContainer.begin(), valueContainer.end(), time, CompareLowerBoundValueAndTime() );
  
      upper =  std::upper_bound(valueContainer.begin(), valueContainer.end(), time, CompareUpperBoundValueAndTime() ); // Crashing here.
  
      std::cout &lt;&lt; "Lower bound: " &lt;&lt; lower - valueContainer.begin() &lt;&lt; std::endl;
      std::cout &lt;&lt; "Upper bound: " &lt;&lt; upper - valueContainer.begin() &lt;&lt; std::endl;
  
     return lower - valueContainer.begin();
  }
  
  
  
  int main()
  {
      MyClass a;
      a.PrintValueContainer();
      std::cout &lt;&lt; "Location received for 2: "  &lt;&lt; a.GetLocationForTime(2) &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>