upper_bound() lower_bound() 用法

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ///第一种
    int a[5]={1,2,4,4,6};
    int it1,it2;
    it1=lower_bound(a,a+5,4)-a;
    it2=upper_bound(a,a+5,4)-a;
    printf("%d %d\n",it1,a[it1]);
    // /*输出*/ 2 4
    printf("%d %d\n",it2,a[it2]);
    // /*输出*/ 4 6

    ///第二种
    vector<int>vec;
    vec.push_back(1),vec.push_back(2);
    vec.push_back(4),vec.push_back(4);
    vec.push_back(6);
    
    vector<int>::iterator it11,it22;
    it11=lower_bound(vec.begin(),vec.end(),4);
    it22=upper_bound(vec.begin(),vec.end(),4);
    it1=lower_bound(vec.begin(),vec.end(),4)-vec.begin();
    it2=upper_bound(vec.begin(),vec.end(),4)-vec.begin();
    printf("%d %d\n",it1,*it11);
    // /*输出*/ 2 4
    printf("%d %d\n",it2,*it22);
    // /*输出*/ 4 6

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zquzjx/p/8835425.html