fork download
  1. #include <vector>
  2. #include <utility>
  3. #include <algorithm>
  4. #include <iostream>
  5.  
  6. template<class Iter, class T>
  7. Iter binary_find(Iter begin, Iter end, T val)
  8. {
  9. // Finds the lower bound in at most log(last - first) + 1 comparisons
  10. Iter i = std::lower_bound(begin, end, val);
  11.  
  12. if (i != end && *i == val)
  13. return i; // found
  14. else
  15. return end; // not found
  16. }
  17.  
  18. int main()
  19. {
  20. std::vector< std::pair<int, int> > vec = { { 1, 2 }, { 2, 2 }, { 3, 4 }, { 4, 6 } };
  21. auto elem = binary_find(vec.begin(), vec.end(), std::make_pair(2, 2));
  22. std::cout << (elem != vec.end()) << std::endl;
  23.  
  24. return 0;
  25. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
1