fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. int main() {
  6. const std::vector<int> v {4, 8, 15, 15, 16, 23, 42};
  7.  
  8. for (int i : {0, 4, 14, 15, 16, 42, 50 }) {
  9. auto it = std::lower_bound(v.begin(), v.end(), i, std::less<>{});
  10. std::cout << i << " (l):" << std::distance(v.begin(), it) << " " ;
  11. auto uit = std::upper_bound(v.rbegin(), v.rend(), i, std::greater<>{}).base();
  12. std::cout << "(u):" << std::distance(v.begin(), uit) << std::endl;
  13. }
  14. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
0 (l):0 (u):0
4 (l):0 (u):0
14 (l):2 (u):2
15 (l):2 (u):2
16 (l):4 (u):4
42 (l):6 (u):6
50 (l):7 (u):7