mercredi 26 mai 2021

Lower and Upper Bound in case of Decreasing/Non-ascending vector

I understood the concept of Lower and Upper found for an Increasing/Ascending array. i.e

1)Lower Bound: iterator pointing to the first element in the range [first, last) >= Value
2)Upper Bound: iterator pointing to the first element in the range [first, last) > Value

Below is my code for Decreasing/Non-ascending vector in which I am facing issue:

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    vector<int> vec = {45,40,35,12,6,3};

    auto itr3 = lower_bound(vec.begin(),vec.end(),40);
    auto itr4 = upper_bound(vec.begin(),vec.end(),40);

    if(itr3 == vec.end() && itr4 == vec.end())
        cout<<"Lower & Upper Bound not Found\n";
    else
    {
        cout <<"lower Bound of 40 :"<<*itr3<<endl;
        cout <<"Upper Bound of 40 :"<<*itr4<<endl;
    }

    return 0;
}

The Output is: Lower & Upper Bound not Found.

But as mentioned above the output should be something like :

lower Bound of 40 :40

Upper Bound of 40 :45

Please help me understood his behavior of lower and upper bound in case Decreasing/Non-ascending vectors.

Thanks in Advance.

Aucun commentaire:

Enregistrer un commentaire