upper_bound()和lower_bound()的使用

来源:互联网 发布:2017三季度gdp数据 编辑:程序博客网 时间:2024/06/05 11:15


#include <iostream>

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int a[100],b[100];int main(){    int n;    while(cin>>n)    {        for(int i=0;i<n;i++)            cin>>a[i];        int t=upper_bound(a,a+n,3)-a; //返回大于数组中最后一个3的第一个元素的位置;        int tt=lower_bound(a,a+n,3)-a;//返回小于数组中第一个3的元素的位置;        cout<<t<<" "<<tt<<endl;    }    return 0;}/*   例如 2 2 2 4 4  t=3  tt=3;   例如 2 2 4 4 4  t=2  tt=2;   例如 2 2 3 4 4  t=3  tt=2;*/