【发布时间】:2016-08-04 18:43:09
【问题描述】:
下面是代码和
std::string str[5] = {"Tejas","Mejas","Rajas","Pojas","Ljas"};
std::sort(str,str+5);
size_t test = bin_search("Ljas",str,5);
这里是二分查找的通用函数
template<class T>
size_t bin_search(T x, T* array, int n)
{
size_t begin = 0, end = n;
// Invariant: This function will eventually return a value in the range [begin, end]
while (begin != end) {
size_t mid = (begin + end) / 2;
if (array[mid] < x) {
begin = mid + 1;
} else {
end = mid;
}
}
return begin; // Or return end, because begin == end
}
错误是
main.cpp|12|error: no matching function for call to 'bin_search(const char [5], std::string [5], int)'|
只有std::string 数组有问题,但int 数组工作得很好。
它适用于字符串数组还是逻辑中缺少任何东西?
【问题讨论】:
标签: c++ arrays string templates c++11