自定义二叉查找模板.cpp 326 B

1234567891011121314
  1. //cmp(compare function):from front to rear
  2. template<typename _T>
  3. int BinarySearchInc(_T* x, _T target, bool* cmp(_T, _T), int start = 0, int end) {
  4. int L = start, R = end, M, res = -1;
  5. while (L <= R) {
  6. M = (L + R) >> 1;
  7. if (cmp(x[M], target))
  8. res = M,
  9. R = M - 1;
  10. else
  11. L = M + 1;
  12. }
  13. return res;
  14. }