我有以下程序在上限调用时崩溃。我不明白为什么会发生崩溃。我崩溃的任何原因。感谢您的帮助和时间。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
enum quality { good = 0, bad, uncertain };
struct sValue {
int time;
int value;
int qual;
};
struct CompareLowerBoundValueAndTime {
bool operator()( const sValue& v, int time ) const {
return v.time < time;
}
bool operator()( const sValue& v1, const sValue& v2 ) const {
return v1.time < v2.time;
}
bool operator()( int time1, int time2 ) const {
return time1 < time2;
}
bool operator()( int time, const sValue& v ) const {
return time < v.time;
}
};
struct CompareUpperBoundValueAndTime {
bool operator()( const sValue& v, int time ) const {
return v.time > time;
}
bool operator()( const sValue& v1, const sValue& v2 ) const {
return v1.time > v2.time;
}
bool operator()( int time1, int time2 ) const {
return time1 > time2;
}
bool operator()( int time, const sValue& v ) const {
return time > v.time;
}
};
class MyClass {
public:
MyClass() {
InsertValues();
}
void InsertValues();
int GetLocationForTime(int time);
void PrintValueContainer();
private:
vector<sValue> valueContainer;
};
void MyClass::InsertValues() {
for(int num = 0; num < 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 < valueContainer.size(); i++) {
std::cout << i << ". " << valueContainer[i].time << std::endl;
}
}
int MyClass::GetLocationForTime(int time)
{
std::vector< sValue >::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 << "Lower bound: " << lower - valueContainer.begin() << std::endl;
std::cout << "Upper bound: " << upper - valueContainer.begin() << std::endl;
return lower - valueContainer.begin();
}
int main()
{
MyClass a;
a.PrintValueContainer();
std::cout << "Location received for 2: " << a.GetLocationForTime(2) << std::endl;
return 0;
}