upper_bound () lower_bound () usage

 

#include <bits/stdc++.h>
using namespace std;
int main()
{
    /// The first 
    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]);
     // /*output*/ 2 4 
    printf( " %d %d\n " ,it2,a[it2]);
     // / *output*/ 4 6

    /// The second type of 
    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);
     // /*output*/ 2 4 
    printf( " %d %d\n " ,it2,* it22);
     // /*output*/ 4 6

    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324403353&siteId=291194637