C++ STL 中lower_bound() 和 upper_bound()讲解

来源:互联网 发布:linux找到文件夹位置 编辑:程序博客网 时间:2024/05/20 04:15

1、lower_bound()

在有序的范围内(具有大于或等于指定值的值)查找第一个元素的位置,该排序标准可由二进制谓词指定。

 语法:

 template<class ForwardIterator, class Type>   ForwardIterator lower_bound(      ForwardIterator first,       ForwardIterator last,      const Type& value   );template<class ForwardIterator, class Type, class BinaryPredicate>   ForwardIterator lower_bound(      ForwardIterator first,       ForwardIterator last,      const Type& value,      BinaryPredicate comp   );
   参数:
   first     寻址要搜索的范围中的第一个元素的位置的向前迭代器。   last     寻址要搜索的范围中最后一个元素的下一位置的向前迭代器。   value     在排序范围中搜索的处于第一个位置或可能处于第一个位置的值。   comp     用户定义的谓词函数对象定义一个元素小于另一个。二进制谓词采用两个参数,并且在满足时返回 true,在未满足时返回 false。
   返回值:
     向前迭代器在有序的范围内(具有大于或等于指定值的值)的第一个元素的位置,其中等效性由二进制谓词指定。
   例子:
// alg_lower_bound.cpp// compile with: /EHsc#include <vector>#include <algorithm>#include <functional>      // greater<int>( )#include <iostream>// Return whether modulus of elem1 is less than modulus of elem2bool mod_lesser( int elem1, int elem2 ){    if ( elem1 < 0 )        elem1 = - elem1;    if ( elem2 < 0 )        elem2 = - elem2;    return elem1 < elem2;}int main( ){    using namespace std;    vector<int> v1;    // Constructing vector v1 with default less-than ordering    for ( auto i = -1 ; i <= 4 ; ++i )    {        v1.push_back(  i );    }    for ( auto ii =-3 ; ii <= 0 ; ++ii )    {        v1.push_back(  ii  );    }    cout << "Starting vector v1 = ( " ;    for (const auto &Iter : v1)        cout << Iter << " ";    cout << ")." << endl;    sort(v1.begin(), v1.end());    cout << "Original vector v1 with range sorted by the\n "        << "binary predicate less than is v1 = ( " ;    for (const auto &Iter : v1)        cout << Iter << " ";    cout << ")." << endl;    // Constructing vector v2 with range sorted by greater    vector<int> v2(v1);    sort(v2.begin(), v2.end(), greater<int>());    cout << "Original vector v2 with range sorted by the\n "        << "binary predicate greater is v2 = ( " ;    for (const auto &Iter : v2)        cout << Iter << " ";    cout << ")." << endl;    // Constructing vectors v3 with range sorted by mod_lesser    vector<int> v3(v1);    sort(v3.begin(), v3.end(), mod_lesser);    cout << "Original vector v3 with range sorted by the\n "        <<  "binary predicate mod_lesser is v3 = ( " ;    for (const auto &Iter : v3)        cout << Iter << " ";    cout << ")." << endl;    // Demonstrate lower_bound    vector<int>::iterator Result;    // lower_bound of 3 in v1 with default binary predicate less<int>()    Result = lower_bound(v1.begin(), v1.end(), 3);    cout << "The lower_bound in v1 for the element with a value of 3 is: "        << *Result << "." << endl;    // lower_bound of 3 in v2 with the binary predicate greater<int>( )    Result = lower_bound(v2.begin(), v2.end(), 3, greater<int>());    cout << "The lower_bound in v2 for the element with a value of 3 is: "        << *Result << "." << endl;    // lower_bound of 3 in v3 with the binary predicate  mod_lesser    Result = lower_bound(v3.begin(), v3.end(), 3,  mod_lesser);    cout << "The lower_bound in v3 for the element with a value of 3 is: "        << *Result << "." << endl;}


   结果:
Starting vector v1 = ( -1 0 1 2 3 4 -3 -2 -1 0 ).Original vector v1 with range sorted by the binary predicate less than is v1 = ( -3 -2 -1 -1 0 0 1 2 3 4 ).Original vector v2 with range sorted by the binary predicate greater is v2 = ( 4 3 2 1 0 0 -1 -1 -2 -3 ).Original vector v3 with range sorted by the binary predicate mod_lesser is v3 = ( 0 0 -1 -1 1 -2 2 -3 3 4 ).The lower_bound in v1 for the element with a value of 3 is: 3.The lower_bound in v2 for the element with a value of 3 is: 3.The lower_bound in v3 for the element with a value of 3 is: -3.


2、upper_bound()
在有序的范围内(具有大于指定值的值)查找第一个元素的位置,该排序标准可由二进制谓词指定。
   语法:
template<class ForwardIterator, class Type>   ForwardIterator upper_bound(      ForwardIterator first,       ForwardIterator last,      const Type& value   );template<class ForwardIterator, class Type, class Predicate>   ForwardIterator upper_bound(      ForwardIterator first,       ForwardIterator last,      const Type& value,      Predicate comp   );
   参数:
first要搜索的范围中的第一个元素的位置。last超过要搜索的范围中的最后一个元素的位置。value顺序范围内的值需要被返回迭代器解决的元素值超过。comp用户定义的谓词函数对象定义一个元素小于另一个。二进制谓词采用两个参数,并且在满足时返回 true,在未满足时返回 false。
   返回值:
在其值设置为大于指定值的第一个元素位置上的向前迭代器。
   例子:
// alg_upper_bound.cpp// compile with: /EHsc#include <vector>#include <algorithm>#include <functional>      // greater<int>( )#include <iostream>// Return whether modulus of elem1 is less than modulus of elem2bool mod_lesser( int elem1, int elem2 ){    if ( elem1 < 0 )        elem1 = - elem1;    if ( elem2 < 0 )        elem2 = - elem2;    return elem1 < elem2;}int main( ){    using namespace std;    vector<int> v1;    // Constructing vector v1 with default less-than ordering    for ( auto i = -1 ; i <= 4 ; ++i )    {        v1.push_back(  i );    }    for ( auto ii =-3 ; ii <= 0 ; ++ii )    {        v1.push_back(  ii  );    }    cout << "Starting vector v1 = ( " ;    for (const auto &Iter : v1)        cout << Iter << " ";    cout << ")." << endl;    sort(v1.begin(), v1.end());    cout << "Original vector v1 with range sorted by the\n "        << "binary predicate less than is v1 = ( " ;    for (const auto &Iter : v1)        cout << Iter << " ";    cout << ")." << endl;    // Constructing vector v2 with range sorted by greater    vector<int> v2(v1);    sort(v2.begin(), v2.end(), greater<int>());    cout << "Original vector v2 with range sorted by the\n "        << "binary predicate greater is v2 = ( " ;    for (const auto &Iter : v2)        cout << Iter << " ";    cout << ")." << endl;    // Constructing vectors v3 with range sorted by mod_lesser    vector<int> v3(v1);    sort(v3.begin(), v3.end(), mod_lesser);    cout << "Original vector v3 with range sorted by the\n "        <<  "binary predicate mod_lesser is v3 = ( " ;    for (const auto &Iter : v3)        cout << Iter << " ";    cout << ")." << endl;    // Demonstrate upper_bound    vector<int>::iterator Result;    // upper_bound of 3 in v1 with default binary predicate less<int>()    Result = upper_bound(v1.begin(), v1.end(), 3);    cout << "The upper_bound in v1 for the element with a value of 3 is: "        << *Result << "." << endl;    // upper_bound of 3 in v2 with the binary predicate greater<int>( )    Result = upper_bound(v2.begin(), v2.end(), 3, greater<int>());    cout << "The upper_bound in v2 for the element with a value of 3 is: "        << *Result << "." << endl;    // upper_bound of 3 in v3 with the binary predicate  mod_lesser    Result = upper_bound(v3.begin(), v3.end(), 3,  mod_lesser);    cout << "The upper_bound in v3 for the element with a value of 3 is: "        << *Result << "." << endl;}

结果:
Starting vector v1 = ( -1 0 1 2 3 4 -3 -2 -1 0 ).Original vector v1 with range sorted by the binary predicate less than is v1 = ( -3 -2 -1 -1 0 0 1 2 3 4 ).Original vector v2 with range sorted by the binary predicate greater is v2 = ( 4 3 2 1 0 0 -1 -1 -2 -3 ).Original vector v3 with range sorted by the binary predicate mod_lesser is v3 = ( 0 0 -1 -1 1 -2 2 -3 3 4 ).The upper_bound in v1 for the element with a value of 3 is: 4.The upper_bound in v2 for the element with a value of 3 is: 2.The upper_bound in v3 for the element with a value of 3 is: 4.


0 0