#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int a[5]={1,2,3,4,5};
int b[5]={1,3,4,5,2};
int *pos;
pos=lower_bound(a,a+5,2);//lower_bound取的是第一个不小于‘2’(指定值)的元素的地址
int idx=distance(a,pos);//用distance计算首地址与其的位置差
cout<<idx<<endl;//距离为1
cout<<a[idx]<<endl;//pos的值为
pos=lower_bound(b,b+5,2);//lower_bound取的是第一个不小于‘2’(指定值)的元素的地址
idx=distance(b,pos);//用distance计算首地址与其的位置差
cout<<idx<<endl;
cout<<b[idx]<<endl;//pos的值为
//lower_bound取的是按照当前序列取第一个不小于(指定值)的元素的地址
return 0;
}
类似的
binary_search upper_bound