共用方式為


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

傳回值

包含的值大於指定值的第一個項目位置的正向順向迭代器。

備註

參考的已排序來源範圍必須是有效的。所有迭代器都必須是可取值的,而且序列中的最後一個位置必須可從第一個位置透過遞增方式到達。

排序的範圍是使用 upper_bound 的前置條件,而且排序準則與二進位述詞所指定的準則相同。

upper_bound 不會修改這個範圍。

向前迭代器的實值類型必須是小於比較才能排序,因此若提供了兩個項目,可以判斷它們相等 (任一個都不小於另一個的意義),或者一個小於另一個。 這會導致在非對等元件之間的排序

演算法的複雜度為隨機存取的對數,若步驟數目成比例則為線性 (last - first)。

範例

// 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 elem2
bool 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;
}

Output

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.

需求

標頭:<algorithm>

命名空間: std

請參閱

參考

lower_bound

equal_range

binary_search

upper_bound 的述詞版本

upper_bound (STL 範例)

標準樣板程式庫