fork download
  1. #include <iostream> // std::cout
  2. #include <algorithm> // std::lower_bound, std::upper_bound, std::sort
  3. #include <vector> // std::vector
  4.  
  5. template<class ForwardIt, class T>
  6. T upperBoundIndex(ForwardIt first, ForwardIt last, const T& value)
  7. {
  8. return (std::upper_bound (first, last, value) - first);
  9. }
  10.  
  11. int main () {
  12. int myints[] = {10,20,30,30,20,10,10,20};
  13. std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20
  14.  
  15. std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
  16.  
  17. std::vector<int>::iterator low,up;
  18. low=std::lower_bound (v.begin(), v.end(), 20); // ^
  19. up= std::upper_bound (v.begin(), v.end(), 20); // ^
  20.  
  21. std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
  22. std::cout << "upper_bound at position " << (up - v.begin()) << '\n';
  23.  
  24. std::cout << "Using upperBoundIndex() : " << upperBoundIndex(v.begin(), v.end(), 20);
  25. }
  26.  
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
lower_bound at position 3
upper_bound at position 6
Using upperBoundIndex() :  6