共用方式為


lower_bound

尋找第一個項目的位置是值大於或等於指定值,排序準則可能是由二進位述詞指定的已排序的範圍。

template<class ForwardIterator, class Type>
   ForwardIterator lower_bound(
      ForwardIterator _First, 
      ForwardIterator _Last,
      const Type& _Val
   );
template<class ForwardIterator, class Type, class BinaryPredicate>
   ForwardIterator lower_bound(
      ForwardIterator _First, 
      ForwardIterator _Last,
      const Type& _Val,
      BinaryPredicate _Comp
   );

參數

  • _First
    處理順向 Iterator 的第一個項目的位置會搜尋的範圍。

  • _Last
    處理順向的 Iterator 超過最後一個項目的位置是在要搜尋的範圍。

  • _Val
    第一個位置或可能的第一個位置中搜尋這個已排序的範圍的值。

  • _Comp
    定義感受遠遠之使用者定義的述詞函式物件哪一個項目小於另一個。 一個二進位述詞採用兩個引數並傳回 true ,當內容和 false ,則內容。

傳回值

順向 Iterator 在第一個項目的位置會以大於或等於指定值的值的已排序的範圍相等,指定具有一個二進位述詞。

備註

參考的已排序資料來源範圍必須是有效的,所有 Iterator 必須 dereferenceable,並在序列中最後一個位置必須是可取得的開頭會增加。

已排序的範圍都是使用 lower_bound 的先決條件,然後排程與指定使用二元述詞的區域。

演算法不會修改這個範圍 lower_bound

正向 iterator 可用於將實值型別必須小於可比較將已排序,則,因此將兩個項目,可以判斷或其相等 (因為都比其他不小於) 或是小於另一個。 這會導致排程在非對等的項目。

演算法的複雜則是指定要為隨機存取 Iterator 以線性的方式,與步驟數比例調整 (_Last1 – _First1)。

範例

// alg_lower_bound.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>      // For 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;
   vector <int>::iterator Iter1, Result1;

   // Constructing vectors v1a & v1b with default less than ordering
   int i;
   for ( i = -1 ; i <= 4 ; i++ )
   {
      v1.push_back(  i );
   }

   int ii;
   for ( ii =-3 ; ii <= 0 ; ii++ )
   {
      v1.push_back(  ii  );
   }

   sort ( v1.begin ( ) , v1.end ( ) );
   cout << "Original vector v1 with range sorted by the\n "
        << "binary predicate less than is  v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   // Constructing vectors v2 with range sorted by greater
   vector <int> v2 ( v1 );
   vector <int>::iterator Iter2, Result2;
   sort ( v2.begin ( ) , v2.end ( ) , greater<int> ( ) );

   cout << "Original vector v2 with range sorted by the\n "
        << "binary predicate greater is    v2 = ( " ;
   for ( Iter2 = v2.begin ( ) ; Iter2 != v2.end ( ) ; Iter2++ )
      cout << *Iter2 << " ";
   cout << ")." << endl;

   // Constructing vectors v3 with range sorted by mod_lesser
   vector <int> v3 ( v1 );
   vector <int>::iterator Iter3, Result3;
   sort ( v3.begin ( ) , v3.end ( ) , mod_lesser );

   cout << "Original vector v3 with range sorted by the\n "
        <<  "binary predicate mod_lesser is v3 = ( " ;
   for ( Iter3 = v3.begin ( ) ; Iter3 != v3.end ( ) ; Iter3++ )
      cout << *Iter3 << " ";
   cout << ")." << endl;

   // lower_bound of 3 in v1 with default binary predicate less <int> ( )
   Result1 = lower_bound ( v1.begin ( ) , v1.end ( ) , 3 );
   cout << "The lower_bound in v2 for the element with a value of 3 is: "
        << *Result1 << "." << endl;

   // lower_bound of 3 in v2 with the binary predicate greater <int> ( )
   Result2 = lower_bound ( v2.begin ( ) , v2.end ( ) , 3, greater <int> ( ) );
   cout << "The lower_bound in v2 for the element with a value of 3 is: "
        << *Result2 << "." << endl;

   // lower_bound of 3 in v3 with the binary predicate  mod_lesser
   Result3 = lower_bound ( v3.begin ( ) , v3.end ( ) , 3,  mod_lesser  );
   cout << "The lower_bound in v3 for the element with a value of 3 is: "
        << *Result3 << "." << endl;
}
  
  
  
  
  
  

需求

標題: <algorithm>

命名空間: std

請參閱

參考

lower_bound (STL Samples)

Predicate Version of lower_bound

標準樣板程式庫