c++ 这题什么意思?怎么写?// Recursive binary search via pointers. If the target is found, return a pointer to it. // Otherwise return NULL.template int * binarySearch(T * firstPtr, T * lastPtr, const T &target);// Top-level binary search func
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/05 12:57:27
![c++ 这题什么意思?怎么写?// Recursive binary search via pointers. If the target is found, return a pointer to it. // Otherwise return NULL.template int * binarySearch(T * firstPtr, T * lastPtr, const T &target);// Top-level binary search func](/uploads/image/z/2730494-38-4.jpg?t=c%2B%2B+%E8%BF%99%E9%A2%98%E4%BB%80%E4%B9%88%E6%84%8F%E6%80%9D%3F%E6%80%8E%E4%B9%88%E5%86%99%3F%2F%2F+Recursive+binary+search+via+pointers.+If+the+target+is+found%2C+return+a+pointer+to+it.+%2F%2F+Otherwise+return+NULL.template+int+%2A+binarySearch%28T+%2A+firstPtr%2C+T+%2A+lastPtr%2C+const+T+%26target%29%3B%2F%2F+Top-level+binary+search+func)
c++ 这题什么意思?怎么写?// Recursive binary search via pointers. If the target is found, return a pointer to it. // Otherwise return NULL.template int * binarySearch(T * firstPtr, T * lastPtr, const T &target);// Top-level binary search func
c++ 这题什么意思?怎么写?
// Recursive binary search via pointers. If the target is found, return a pointer to it. // Otherwise return NULL.template
int * binarySearch(T * firstPtr, T * lastPtr, const T &target);// Top-level binary search function. Calls the function above.
// Return index of target in values or -1, if target isn't found.template int binarySearch(T values[], int size, const T &target) { int * loc = binarySearch(values, values+size-1, target); return loc == NULL ? -1 : loc-values;
}
c++ 这题什么意思?怎么写?// Recursive binary search via pointers. If the target is found, return a pointer to it. // Otherwise return NULL.template int * binarySearch(T * firstPtr, T * lastPtr, const T &target);// Top-level binary search func
二分搜索啦!
//假定升序排列
template
int *binarySearch(T* fistPtr,T* lastPtr,const T& target)
{
T *middle=(fistPtr+lastPtrt)>>1;
if (*middle==target)
{
return middle;
}
if (fistPtr==lastPtr)
return null;
if (*middle