lexicographical_compare

由兩個序列之間的項目比較項目決定哪些是較小的兩個。

template<class InputIterator1, class InputIterator2>
   bool lexicographical_compare(
      InputIterator1 _First1,
      InputIterator1 _Last1,
      InputIterator2 _First2,
      InputIterator2 _Last2
   );
template<class InputIterator1, class InputIterator2, class BinaryPredicate>
   bool lexicographical_compare(
      InputIterator1 _First1,
      InputIterator1 _Last1, 
      InputIterator2 _First2, 
      InputIterator2 _Last2,
      BinaryPredicate _Comp
   );

參數

  • _First1
    處理輸入的 Iterator 的第一個項目位置在要比較的第一個範圍。

  • _Last1
    處理輸入的 Iterator 超過最後一個項目的位置是在要比較的第一個範圍。

  • _First2
    處理輸入的 Iterator 的第一個項目位置在要比較的第二個範圍。

  • _Last2
    處理輸入的 Iterator 超過最後一個項目的位置是在要比較的第二個範圍。

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

傳回值

true ,如果第一個範圍大於第二個範圍 Dictionary 地小於;否則 false

備註

在序列之間的字典相比較。項目進行比較之前遇到的項目:

  • 找到兩個相對應的項目,不相等,而且其比較的結果會因比較在序列之間。

  • 找不到不等比較,不過,一個序列比另一個有多個項目,因此,這個序列較短的較長序列會視為小於。

  • 找不到不相等,且序列沒有相同的元素數目,因此,序列,兩者才會相等比較的結果為 false。

範例

// alg_lex_comp.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>

// Return whether second element is twice the first
bool twice ( int elem1, int elem2 )
{
   return 2 * elem1 < elem2;
}

int main( )
{
   using namespace std;
   vector <int> v1, v2;
   list <int> L1;
   vector <int>::iterator Iter1, Iter2;
   list <int>::iterator L1_Iter, L1_inIter;

   int i;
   for ( i = 0 ; i <= 5 ; i++ )
   {
      v1.push_back( 5 * i );
   }
   int ii;
   for ( ii = 0 ; ii <= 6 ; ii++ )
   {
      L1.push_back( 5 * ii );
   }

   int iii;
   for ( iii = 0 ; iii <= 5 ; iii++ )
   {
      v2.push_back( 10 * iii );
   }

   cout << "Vector v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;

   cout << "List L1 = ( " ;
   for ( L1_Iter = L1.begin( ) ; L1_Iter!= L1.end( ) ; L1_Iter++ )
      cout << *L1_Iter << " ";
   cout << ")" << endl;

   cout << "Vector v2 = ( " ;
   for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
      cout << *Iter2 << " ";
      cout << ")" << endl;

   // Self lexicographical_comparison of v1 under identity
   bool result1;
   result1 = lexicographical_compare (v1.begin( ), v1.end( ),
                  v1.begin( ), v1.end( ) );
   if ( result1 )
      cout << "Vector v1 is lexicographically_less than v1." << endl;
   else
      cout << "Vector v1 is not lexicographically_less than v1." << endl;

   // lexicographical_comparison of v1 and L2 under identity
   bool result2;
   result2 = lexicographical_compare (v1.begin( ), v1.end( ),
                  L1.begin( ), L1.end( ) );
   if ( result2 )
      cout << "Vector v1 is lexicographically_less than L1." << endl;
   else
      cout << "Vector v1 is lexicographically_less than L1." << endl;

   bool result3;
   result3 = lexicographical_compare (v1.begin( ), v1.end( ),
                  v2.begin( ), v2.end( ), twice );
   if ( result3 )
      cout << "Vector v1 is lexicographically_less than v2 "
           << "under twice." << endl;
   else
      cout << "Vector v1 is not lexicographically_less than v2 "
           << "under twice." << endl;
}
  
  
  

需求

標題: <algorithm>

命名空間: std

請參閱

參考

標準樣板程式庫