//cmp(compare function):from front to rear template int BinarySearchInc(_T* x, _T target, bool* cmp(_T, _T), int start = 0, int end) { int L = start, R = end, M, res = -1; while (L <= R) { M = (L + R) >> 1; if (cmp(x[M], target)) res = M, R = M - 1; else L = M + 1; } return res; }