iterator
是一个自定义类,没有什么花哨的,可以满足您的期望。 vec
是一个迭代器的向量,指向一个更大的链接列表,即“真正”搜索
iterator searchVec(const I& item)
{
int left = 0;
int right = (int)vec.size()-1;
int mid = (right+left)/2;
while(*vec.at(left) != *vec.at(right)){
mid = (right+left)/2;
if (mid == 0 || mid == vec.size()-1){
//nothign else to search, we didnt find anything
return *vec.end();
}
if (*vec.at(mid) == item){
return vec.at(mid);
}
else if (item > *vec.at(mid)){
left = mid;
}
else if (item < *vec.at(mid)){
right = mid;
}
}
return vec.at(mid);
}
答案 0 :(得分:0)
一些想法:
std::vector::at
slow compared to std::vector::operator[]
。vec.end()
。通常,如果您想要通知来电者没有找到任何项目,则会返回该通知。但是在这里,因为你没有返回vec
的迭代器,也许你应该用布尔输出参数或异常来发出信号。left <= right
而不是*vec[left] != *vec[right]
。然后循环的目的是选择项目。如果我们突破循环,则找不到任何项目。退出条件无用,会阻止查找第一个和最后一个元素,如评论中所述。
if (mid == 0 || mid == vec.size()-1)
//nothign else to search, we didnt find anything
return *vec.end();
}
这是一个略微更正的代码:
iterator searchVec(const I& item)
{
int left = 0;
int right = (int)vec.size()-1;
int mid = (right+left)/2;
while(left <= right) // corrected condition
{
mid = (right+left)/2;
iterator it = vec[mid]; // avoid recomputing vec[mid]
if (*it == item) // this can be moved in a else {} statement,
return it; // after the else if (no impact on performances I think)
else if (item > *it)
left = mid + 1; // you were not strictly decreasing the interval
else if (item < *it)
right = mid - 1; // same here
}
throw not_found();
}