fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. vector<int>a= {5,9,10,10,10,10,11,11,12};
  6. // auto it= lower_bound(v.begin(),v.end(),11);
  7. // int idx = it-v.begin();
  8. // implementing lower_bound:
  9. int l=0, h=a.size()-1;
  10. int k=11;
  11. while(l<h){
  12. int m=(l+h)/2;
  13. if(a[m]<k)
  14. l=m+1;
  15. else{
  16. if(a[m]==k)
  17. h=m;
  18. h=m;
  19. }
  20. }
  21. cout<<" idx is "<<h<<endl;
  22. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
 idx is 6