码迷,mamicode.com
首页 > 其他好文 > 详细

lower_bound, upper_bound

时间:2016-01-16 11:52:21      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

lower_bound:

Returns an iterator pointing to the first element in the range [first,last) which does not compare less than val.

 

upper_bound:

Returns an iterator pointing to the first element in the range [first,last) which compares greater than val.

 

int main(int argc, const char * argv[]) {
    int myints[] = {10,20,30,30,20,10,10,20};
    vector<int> v(myints,myints+8);           // 10 20 30 30 20 10 10 20
    
    sort (v.begin(), v.end());                // 10 10 10 20 20 20 30 30
    
    vector<int>::iterator low1,low2, up1, up2, up3;
    low1 = lower_bound (v.begin(), v.end(), 15); //^
    low2 = lower_bound (v.begin(), v.end(), 20); //^
    up1 = lower_bound (v.begin(), v.end(), 20); //^
    up2 = upper_bound (v.begin(), v.end(), 25); //
    up3 = upper_bound (v.begin(), v.end(), 100); //  =v.end()
    
    std::cout << "lower_bound for 15 is " << (low1- v.begin()) << \n;
    std::cout << "lower_bound for 20 is " << (low2 - v.begin()) << \n;
    std::cout << "upper_bound for 20 is " << (up1- v.begin()) << \n;
    std::cout << "upper_bound for 25 is " << (up2 - v.begin()) << \n;
    std::cout << "upper_bound for 100 is " << (up3 - v.begin()) << \n;
    
    return 0;
}

 

Output:

lower_bound for 15 is 3

lower_bound for 20 is 3

upper_bound for 20 is 3

upper_bound for 25 is 6

upper_bound for 100 is 8

lower_bound, upper_bound

标签:

原文地址:http://www.cnblogs.com/XingyingLiu/p/5135054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!