template<typename T>
int binary_search_recursive(std::vector<T> v, T target)
{
int low = 0;
int high = v.size() - 1;
int mid = low + (high - low) / 2;
if (v[0]>target||v[v.size()-1]<target)
return -1;
else
{
if (v[mid] == target)
return mid;
else if (v[mid] < target)
{
low = mid + 1;
std::vector<T>v_low(v.begin() + low, v.end());
return low + binary_search_recursive(v_low, target);
}
else
{
high = mid - 1;
std::vector<T>v_high(v.begin(), v.begin() + high + 1);
return binary_search_recursive(v_high, target);
}
}
}
return low + binary_search_recursive(v_low, target):目标点位于检测点右侧,递归返回的新向量,目标新位置较原向量位置左移(索引值变小),所以需要加上low的位置
return binary_search_recursive(v_high, target):目标位置位于检测点右侧,递归返回的新向量,目标的新位置与原来向量位置的索引未改变,故不需要加偏移量