二分法模板binary_search

来源:互联网 发布:php 数组反转 编辑:程序博客网 时间:2024/06/05 11:28
#include<iostream>#include<algorithm>using namespace std;int main(){    int a[100]= {4,10,11,30,69,70,96,100};    int b=binary_search(a,a+9,4);//查找成功,返回1    cout<<"在数组中查找元素4,结果为:"<<b<<endl;    int c=binary_search(a,a+9,40);//查找失败,返回0    cout<<"在数组中查找元素40,结果为:"<<c<<endl;    int d=lower_bound(a,a+9,10)-a;    cout<<"在数组中查找第一个大于等于10的元素位置,结果为:"<<d<<endl;    int e=lower_bound(a,a+9,101)-a;    cout<<"在数组中查找第一个大于等于101的元素位置,结果为:"<<e<<endl;    int f=upper_bound(a,a+9,10)-a;    cout<<"在数组中查找第一个大于10的元素位置,结果为:"<<f<<endl;    int g=upper_bound(a,a+9,101)-a;    cout<<"在数组中查找第一个大于101的元素位置,结果为:"<<g<<endl;}
原创粉丝点击