#include<iostream>
#include<algorithm>
using namespace std;
bool compare(int a,int b){
cout<<a<<" "<<b<<endl;
return a<=b;
}
int main()
{ int n;
cin>>n;
int a[n]={1,2,5,10,50,100,200,500,2000};
int money=100;
int it =lower_bound(a,a+n,money,compare)-a;
cout<<a[it];
return 0;
}
In this code, I made a custom comparator for lower_bound()
, so it is supposed to output 100 in the end (as in the lst compare 100 <=100 is true). But it gives the next index (200) as an output... why?
Right, just as Yadav said. You can also see cppreference about compare requirement:
comp - binary predicate which returns true if the first argument is less than (ie is ordered before) the second.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.