fork download
  1. // lower_bound/upper_bound example
  2. #include <iostream> // std::cout
  3. #include <algorithm> // std::lower_bound, std::upper_bound, std::sort
  4. #include <vector> // std::vector
  5.  
  6. int main () {
  7. int v[] = {10,20,30,30,20,10,10,20};
  8. int n = 8;
  9. //std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20
  10.  
  11. std::sort (v, v+8); // 10 10 10 20 20 20 30 30
  12.  
  13. //std::vector<int>::iterator low,up;
  14. //low=std::lower_bound (v.begin(), v.end(), 20); // ^
  15. //up= std::upper_bound (v.begin(), v.end(), 20); // ^
  16.  
  17. int * p = std::upper_bound(v,v+n,20);
  18.  
  19. //std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
  20. //std::cout << "upper_bound at position " << (up - v.begin()) << '\n';
  21. if( p == v + n )
  22. std::cout << "No element greater";
  23. else
  24. std::cout << "The first element greater is " << *p
  25. << " at position " << p - v;
  26. return 0;
  27. }
Success #stdin #stdout 0.01s 5424KB
stdin
Standard input is empty
stdout
The first element greater is 30 at position 6