在C 中使用upper_bound时崩溃

crash when using upper_bound in C++

本文关键字:bound 崩溃 upper      更新时间:2023-10-16

我有以下程序,该程序在上限呼叫中崩溃。我没有得到为什么发生崩溃的原因。我崩溃的任何原因。感谢您的帮助和时间。

#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_boundupper_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晚。

您的序列和谓词组合并非如此,因此您会遇到断言错误。