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())
else
{
cout <<"lower Bound of 40 :"<<*itr3<<endl;
cout <<"Upper Bound of 40 :"<<*itr4<<endl;
}

return 0;
}

The Output is:

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++ 869 views

## 1 answer to this question.

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())
else
{
cout <<"lower Bound of 40 :"<<*itr3<<endl;
cout <<"Upper Bound of 40 :"<<*itr4<<endl;
}

return 0;
}

• 4,960 points

## Use of min and max functions in C++

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

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

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

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

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

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

## Why would anyone use set instead of unordered_set?

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