lower_bound与upper_bound函数

lower_bound与upper_bound函数

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;	
}
发布了954 篇原创文章 · 获赞 307 · 访问量 112万+

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/104107864