lower_bound()
lower_bound(begin,end,num):该函数表示,返回数组中第一个大于等于num的数的地址,不存在返回end。返回值减去数组的初始地址等于这个数的下标。
upper_bound()
upper_bound(begin,end,num):该函数表示,返回数组中第一个大于num的数的地址,不存在返回end。返回值减去数组的初始地址等于这个数的下标。
小demo
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int a[6]={2,64,4,7,9,1};
sort(a,a+6);
int index1 = lower_bound(a,a+6,9)-a;
int index2 = upper_bound(a,a+6,9)-a;
cout << index1 << " " << a[index1] << endl;
cout << index2 << " " << a[index2] << endl;
return 0;
}