1.说明


  • lower_bound,upper_bound使用的前提是数组序列有序(升序降序均可)
  • lower_bound找出序列中第一个大于等于x的数
  • upper_bound找出序列中第一个大于x的 数

2.示例

#include<algorithm>
#include<iostream>
using namespace std;

const int maxN = 10;
int main(){
int arr[maxN] = {23,23,13,12,3};

sort(arr,arr+5);
for(int i = 0;i< 5;i++)
cout << arr[i]<<" ";
cout << endl;
//lower_bound,upper_bound使用的前提是数组序列有序(升序降序均可)
//lower_bound找出序列中第一个大于等于x的数
int index1 = lower_bound(arr,arr+5,23) - arr;//返回下标
cout << index1<<endl;

//upper_bound找出序列中第一个大于x的 数
int index2 = upper_bound(arr,arr+5,12) - arr;
cout << index2 << endl;
}

得到结果如下:

lower_bound与upper_bound函数_数组