简体   繁体   English

cpp- lower_bound返回错误的迭代器

[英]cpp- lower_bound returns the wrong iterator

I have the following class Sudent with a function called isFail(). 我有以下类Sudent,其函数名为isFail()。 This function returns true if there is a grade<=60, and false if not. 如果等级<= 60,则此函数返回true,否则返回false。

class Student
{
    public:
        Student(std::string name_ , int const id_);
        virtual ~Student();
        void addGrade(int const grade2add);//grade above 100
        void removeGrade (int const grade2remove);  //update maxGrade
        bool isFail();//60-fail
        void print(); //id, name, grades

    private:
        std::string name;  //max 20 chars
        int const id; //5 digits, only digits
        std::vector<int> grades;

};

I implement the isFail function: 我实现了isFail函数:

bool Student::isFail()
{
    int temp= *lower_bound(grades.begin(), grades.end(), 61);
    if (temp <= 60)
        return true;
    else
        return false;
}

For some reason I see that in case of the following grased: [100,98,96,60,95] Temp is equal to 100 (the first element) instead of 60. 出于某种原因,我看到在以下情况下:[100,98,96,60,95] Temp等于100(第一个元素)而不是60。

ps all the relevant libraries are included plus using namespace std. ps包含所有相关库以及使用命名空间std。

I read the documentation in http://www.cplusplus.com/reference/algorithm/lower_bound/ , and according their example it supposed to work. 我阅读http://www.cplusplus.com/reference/algorithm/lower_bound/中的文档,根据他们的示例,它应该可以工作。

IF there is better way to do it please suggest. 如果有更好的方法,请建议。

std::lower_bound is returning the right iterator, it just doesn't do what you want. std::lower_bound正在返回正确的迭代器,它只是没有做你想要的。 It returns the first element that is not less than the value. 它返回不小于该值的第一个元素。 In your case, the first element of your vector [100,98,96,60,95] is 100. 在您的情况下,矢量[100,98,96,60,95]的第一个元素是100。

Maybe take a look at std::min_element instead? 也许看看std::min_element代替? After all the minimum element seems to be what you are looking for. 毕竟最小元素似乎是你正在寻找的。 http://en.cppreference.com/w/cpp/algorithm/min_element http://en.cppreference.com/w/cpp/algorithm/min_element

From the documentation of std::lower_bound 来自std::lower_bound的文档

Returns an iterator pointing to the first element in the range [first, last) that is not less than (ie greater or equal to) value. 返回指向范围[first,last]中不小于(即大于或等于)值的第一个元素的迭代器。

So, lower_bound seems not to be what you are looking for for an unsorted array. 因此, lower_bound似乎不是您正在寻找的未排序数组。 If you would like to use lower_bound anyways, you should also consider that the doc says: 如果您仍想使用lower_bound ,您还应该考虑该文档说:

The range [first, last) must be at least partially ordered 范围[first,last]必须至少部分排序

If you want to find the element that is closest to 61 but lower than 61, you should either sort your array first or write an own algorithm that would keep track of the closest element but had to go through all numbers. 如果要查找最接近61但低于61的元素,则应首先对数组进行排序,或者编写自己的算法来跟踪最接近的元素但必须遍历所有数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM