#include<bits/stdc++.h>
using namespace std;
int num[6]={7,2,4,1,15,34};
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int temp1[6],temp2[6];
memcpy(temp1,num,sizeof(temp1));
memcpy(temp2,num,sizeof(temp2));
sort(temp1,temp1+6,cmp);
//求第一个小于7的数
int pos1=upper_bound(temp1,temp1+6,7,greater<int>())-temp1;
cout<<pos1<<' '<<temp1[pos1]<<endl;
//求第一个小于或等于7的数
int pos2=lower_bound(temp1,temp1+6,7,greater<int>())-temp1;
cout<<pos2<<' '<<temp1[pos2]<<endl;
sort(temp2,temp2+6);
//求第一个大于7的数
int pos3=upper_bound(temp2,temp2+6,7)-temp2;
cout<<pos3<<' '<<temp2[pos3]<<endl;
//求第一个大于或等于7的数
int pos4=lower_bound(temp2,temp2+6,7)-temp2;
cout<<pos4<<' '<<temp2[pos4]<<endl;
return 0;
}
upper_bound与lower_bound
最新推荐文章于 2025-12-18 17:00:31 发布
9万+

被折叠的 条评论
为什么被折叠?



