STL中二分查找函数——binary_search//lower_bound//upper_bound//equal_range
以下函数均需配合头文件< algorithm>和sort函数排序后使用 lower_bound int lower_bound(start,end,num); 返回值是int类型,返回的是一个指针,意义是在[start,end)范围内第一次遇到 大于或等于 num的下标。 upper_bound int upper_bound(start,end,num); 返回值是int类型,返回的是一个指针,意义是在[start,end)范围内第一次遇到 大于却不等于 num的下标。 binary_search bool binary_search(start,end,num); 返回值是bool类型,意义是在[start,end)范围内是否有num equal_range pair upper_bound(start,end,num); 返回值是pair类型,返回pair< i , j > 。意义是在[ i , j )区间上的数都等于num,相当于是lower_bound和upper_bound的结合 以上函数都是以二分为实现在已排好序的数组或容器中查找的函数,当返回值意义各不相同 (图片来自https://blog.csdn.net/u010700335/article/details/41323427) 来源: CSDN 作者: TSD_captain 链接: https://blog