Lower bound function

I don’t understand how lower bound fun works someone telling it return value which is greater or equal to key value but it not
you can see my code here and run it on your own compiler
#include <bits/stdc++.h>
using namespace std;
bool comp(int a,int b){
cout<<"comparing “<<a<<” and "<<b<<endl;
return a>=b;
}

int main() {
int a[]={1,2,5,10,20,50,100,100,100,500,1000,2000};
int n=sizeof(a)/sizeof(int);
int key=100;
// cin>>key;
auto lb=lower_bound(a,a+n,key,comp)-a;
//cout<<“index”<<lb<<" at"<<a[lb]<<endl;
cout<<lb;

return 0;

}

hello @ankit_verma
you are using ur own comparator in lower bound function thats why it is working differently,
remove that comparator and then it will work as usual i.e give value greater than equal to key.

ya that’s the point i am using my own comparator in the same manner of what lower bound fun do so why i get diffrent ans??

lower bound comparator is defined something as->
bool comp(int a,int b){
return a<b;
}

how the values of a and b are passing bcz i am still confusing to understand. we tells that it will return something >= but we sad that it returns (a<b) .please tell me

using a<=b, comparator function returns true for all elements <= value to be found. When the comparator function returns false at that time we see that the current index might be the answer. If we dont use comparator function, it returns true only for elements < value to be found.

i didn’t get properly

bro pls rewatch the video, u will get it .

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.