在C 中使用upper_bound时崩溃
crash when using upper_bound in C++
我有以下程序,该程序在上限呼叫中崩溃。我没有得到为什么发生崩溃的原因。我崩溃的任何原因。感谢您的帮助和时间。
#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;
}
lower_bound
和 upper_bound
在排序序列上工作。序列必须使用您传递给两个函数的相同比较函数对序列进行排序。
当您在InsertValues
中插入元素时,您会按升序插入它们,因此CompareLowerBoundValueAndTime
是比较它们的正确方法。
但是,对于upper_bound
,您正在传递其他比较函数。通过CompareLowerBoundValueAndTime()
,它应该起作用。
请注意,CompareLowerBoundValueAndTime
是一个误导性的名称。它应该是CompareValueAndTimeAscending
的线。
您应该对upper_bound和lower_bound使用相同的比较。区别在于算法,而不是比较。
您的编译器正在给您答案。在此处查看您的代码:http://ideone.com/x6re9
这给您一个错误说:
prog.cpp: In member function ‘int MyClass::GetLocationForTime(int)’:
prog.cpp:94: error: no match for ‘operator*’ in ‘*upper.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = sValue*, _Container = std::vector<sValue, std::allocator<sValue> >]()’
您不必两次解除upper
,这没有任何意义。
我认为您在upper_bound中遇到断言错误,因为它发现您的序列未正确排序。
您似乎误解了upper_bound的作用。它与lower_bound相同,除了迭代器指向的项目严格大于搜索值,而不是更大或均等。如果没有这样的值,它将指向序列的末尾。
使用谓词(pred)时,需要对其进行排序,以使
Pred( iter2, iter1 )
每当iter2出现在序列中的iter1时。
中的iTer2晚。您的序列和谓词组合并非如此,因此您会遇到断言错误。
相关文章:
- 当回溯以零开始时,如何调试崩溃
- 内联映射初始化的动态atexit析构函数崩溃
- 执行函数时导致崩溃的变量
- 程序崩溃并显示"std::out_of_range"错误
- CoInitialize()在单独的线程上崩溃而不返回
- 使用调试/崩溃报告将应用程序部署到客户端
- 为什么所有C++编译器都会崩溃或挂起此代码
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 为什么我的多线程作业队列崩溃
- ExtractIconEx:可以工作,但偶尔会崩溃
- 为什么引用传递会导致此崩溃(C++)
- 试图创建流或fopen时程序崩溃
- 类对象数组的问题会导致崩溃
- 排序时无法执行交换操作.我做的时候它会崩溃.为什么
- 为什么要增加导致崩溃的指针
- 在虚幻引擎中删除NXOpen对象时崩溃
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- Visual Studio在尝试读取resource.txt文件时崩溃
- 将方法转换为调用该方法的成员函子对象会导致崩溃