fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. vector<int>a= {2, 3, 7, 10, 11, 11, 25};
  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=9;
  11. int ans = a.size();
  12. while(l<h){
  13. int m=(l+h)/2;
  14. if(a[m]<k)
  15. l=m+1;
  16. else{
  17. if(a[m]==k)
  18. ans=m;
  19. h=m;
  20. }
  21. }
  22. cout<<" idx is "<<ans<<endl;
  23. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
 idx is 7