template<typename comparable> int binarySearch(const vector<comparable> &a, const comparable &x){ int low = 0, high = a.size() - 1; while(low <= high) { int center = (high + low)/2; if(x < a[center]) high = center - 1; else if(x > a[center]) low = center + 1; else return center; } return -1; }
原文:http://www.cnblogs.com/dracohan/p/3825842.html