首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

这种情况,binary_search的第四个参数该怎么写?解决给分

2012-04-14 
这种情况,binary_search的第四个参数该如何写?在线等,解决给分!有一个数组,里面元素是结构体,数组有顺序,s

这种情况,binary_search的第四个参数该如何写?在线等,解决给分!
有一个数组,里面元素是结构体,数组有顺序,
struct{int   i;int   j;}每一个节点实际表示了一个范围(i,i+j)。   我想使用binary_search来搜索index是否存在此数组里面,如果在一个节点的范围内也算是搜索到了。binary_search的第四个参数该如何写?

[解决办法]
函数模版:
template <typename T>
int binary_search (T arr[], int size, T target) ;

参数说明:
T: 模版参数
arr : 数组首地址,
size: 数组元素个数,
T target : 需要查找的值
返回值: 如果数组中找到target, 返回其下标
否则返回 -1
要求数组元素顺序非递减

template <typename T>
int binary_search (T arr[], int size, T target)
...{
int position;
T data;
int bottom = 0, top = size - 1;
while (bottom < top) ...{
int mid = (bottom + top) / 2;
data = arr[mid];
if (data < target)
bottom = mid + 1;
else
top = mid;
}
if (top < bottom) return -1;
else ...{
position = bottom;
data = arr[bottom];
if (data == target) return position;
else return -1;
}
}

[解决办法]
是你自己的数组,还是stl 的 vector 啊

如果是你自己的数组,用不了吧
[解决办法]
是说在一个数组成员的两个数值之间,比如(1,5).
mod_lesser应该这样写吧?
bool inSide(_s elem1,_s elem2)
{
if (elem1.a < elem2.a && elem1.b > elem2.b)
return true;

return false;
}

热点排行