在C ++中使用upper_bound时崩溃

时间:2012-10-19 08:43:35

标签: c++ stl

我有以下程序在上限调用时崩溃。我不知道为什么会发生崩溃。我崩溃的原因。谢谢你的帮助和时间。

#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;
}

4 个答案:

答案 0 :(得分:8)

lower_boundupper_bound处理已排序的序列。必须使用传递给两个函数的相同比较函数对序列进行排序。

当您在InsertValues中插入元素时,会按升序插入这些元素,因此CompareLowerBoundValueAndTime是比较它们的正确方法。

但是对于upper_bound,你传递了一个不同的比较函数。通过CompareLowerBoundValueAndTime(),它应该有效。

请注意,CompareLowerBoundValueAndTime是一个误导性的名称。它应该是CompareValueAndTimeAscending

答案 1 :(得分:1)

您应该对upper_bound和lower_bound使用相同的比较器。不同之处在于算法,而不是比较。

答案 2 :(得分:1)

您的编译器正在给您答案。在此处查看您的代码: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,这没有任何意义。

答案 3 :(得分:0)

我认为你在upper_bound中收到一个断言错误,因为它发现你的序列没有正确排序。

你似乎误解了upper_bound的作用。它与lower_bound相同,只是迭代器指向的项将严格大于搜索值,而不是大于或等于。如果没有这样的值,它将指向序列的结尾。

使用谓词(Pred)时,需要对其进行排序

 Pred( iter2, iter1 )
只要iter2出现在序列中的iter1之后,

将返回false。

您的序列和谓词组合不是这种情况,因此您会收到断言错误。

相关问题