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

0 votes

For an Increasing/Ascending array, I grasped the notion of Lower and Upper discovered.

Lower Bound: iterator pointing to the first element in the [first, last] range >= Value

Upper Bound: iterator pointing to the first element in the [first, last] range > Value

Below is my Decreasing/Non-ascending vector code, which is causing me problems:

#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 assist me in understanding the lower and upper bound behaviour in the situation of decreasing/non-ascending vectors.

Jun 14, 2022 in C++ by Nicholas
• 7,760 points
103 views

1 answer to this question.

0 votes

Both std::lower bound and std::upper bound must have an increasing (non-decreasing) order as their objective.

By giving a comparator as the 4th parameter of the functions, you may modify the meaning of "growing."

To work with descending vectors, use std::greater.

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

using namespace std;

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

    auto itr3 = lower_bound(vec.begin(),vec.end(),40,std::greater<int>());
    auto itr4 = upper_bound(vec.begin(),vec.end(),40,std::greater<int>());

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

answered Jun 14, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

Use of min and max functions in C++

Are std::min and std::max better than fmin ...READ MORE

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
113 views
0 votes
0 answers

Purpose of Unions in C and C++

I had previously used unions with ease, but when I read this post and learned that this code union ARGB { uint32_t colour; ...READ MORE

Jul 13, 2022 in C++ by Nicholas
• 7,760 points
62 views
0 votes
0 answers

Purpose of Unions in C and C++

I had previously utilised unions with ease; however, I was startled when I read this post and discovered that this code union ARGB { uint32_t colour; ...READ MORE

Jul 13, 2022 in C++ by Nicholas
• 7,760 points
68 views
0 votes
1 answer

How to convert an instance of std::string to lower case

#include <algorithm> #include <cctype> #include <string> std::string data = "Abc"; std::transform(data.begin(), ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
369 views
0 votes
1 answer

How do i apply lower_bound to a range of unsorted vector elements?

What's the point of sorting your array? ...READ MORE

answered Jun 15, 2022 in C++ by Damon
• 4,960 points
459 views
0 votes
0 answers

C++ vector erase function not working properly

Here is the code I want to use to delete any entries with values more than 2 and fewer than 5. vector<int> myvector{3, 3, 3, 3, 3, 3, ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
205 views
0 votes
1 answer

How to find out if an item is present in an std::vector?

The most straightforward solution is to count the total number of elements in the vector that have the specified value.  If the count is greater than zero, we've found our element.  This is simple to accomplish with the std::count function. #include <iostream> #include <vector> #include <algorithm> int main() { ...READ MORE

answered May 27, 2022 in Others by Damon
• 4,960 points
564 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
507 views
0 votes
1 answer

Cases of static and dynamic binding in C++

When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class). When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime). Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class. Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding). Derived1 d1(1, 10); d1.display_data(); You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method (). The first condition is incorrect: d1 is neither a pointer nor a reference. The second condition isn't acceptable:  There is no virtual Derived1::display data. The second call is for ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
147 views
0 votes
1 answer

Use of min and max functions in C++

The functions fmin and fmax are designed ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
2,812 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP