共用方式為


includes

已排序的範圍是否包含在第二個排序範圍內的所有項目的測試,在項目之間的順序或相等準則可能是由二進位述詞指定。

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

參數

  • _First1
    處理輸入的 Iterator 第一個項目的位置在第一個排序的來源範圍會測試所有項目的第二個是在第一個中。

  • _Last1
    處理輸入的 Iterator 超過最後一個項目的位置是在第一個排序的來源範圍會測試所有項目的第二個是在第一個中。

  • _First2
    處理輸入的 Iterator 第一個項目的位置在第二個兩個連續的已排序的來源範圍會測試所有項目的第二個是在第一個中。

  • _Last2
    處理輸入的 Iterator 超過最後一個項目的位置是在第二個兩個連續的已排序的來源範圍會測試所有項目的第二個是在第一個中。

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

傳回值

true ,如果第一個排序的範圍在第二個排序範圍內所有項目,否則, false

備註

另一種辨識這個測試是它會判斷為第二個資源範圍是否為第一個來源範圍的子集。

參考的已排序資料來源範圍必須是有效的,任何指標必須 dereferenceable,必要時,在每個序列中,最後一個位置必須是可取得的開頭會增加。

必須將每個已排序的來源範圍時,這個演算法的應用程式的前提是包含與排程相同符合與將演算法使用排序合併的範圍。

演算法不會修改來源範圍 merge

輸入 Iterator 的實值型別必須小於可比較將已排序,則,因此將兩個項目,可以判斷或其相等 (因為都比其他不小於) 或是小於另一個。 這會導致排程在非對等的項目之間。 更明確地說,演算法測試在第一個排序範圍內的任何項目在指定的二進位述詞下是否具有相同的順序對應到第二個排序的範圍。

演算法的複雜度是線性與最多 2 個* ((_Last1 – _First1) (_Last2 – _First2) – 1 非空白的來源範圍比較。

範例

// alg_includes.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> v1a, v1b;
   vector <int>::iterator Iter1a,  Iter1b;

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

   int ii;
   for ( ii =-2 ; ii <= 3 ; ii++ )
   {
      v1b.push_back(  ii  );
   }

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

   cout << "Original vector v1b with range sorted by the\n "
        <<  "binary predicate less than is v1b = ( " ;
   for ( Iter1b = v1b.begin ( ) ; Iter1b != v1b.end ( ) ; Iter1b++ )
      cout << *Iter1b << " ";
   cout << ")." << endl;
   
   // Constructing vectors v2a & v2b with ranges sorted by greater
   vector <int> v2a ( v1a ) , v2b ( v1b );
   vector <int>::iterator Iter2a,  Iter2b;
   sort ( v2a.begin ( ) , v2a.end ( ) , greater<int> ( ) );
   sort ( v2b.begin ( ) , v2b.end ( ) , greater<int> ( ) );
   v2a.pop_back ( );

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

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

   // Constructing vectors v3a & v3b with ranges sorted by mod_lesser
   vector <int> v3a ( v1a ), v3b ( v1b ) ;
   vector <int>::iterator Iter3a, Iter3b;
   reverse (v3a.begin( ), v3a.end( ) );
   v3a.pop_back ( );
   v3a.pop_back ( );
   sort ( v3a.begin ( ) , v3a.end ( ) , mod_lesser );
   sort ( v3b.begin ( ) , v3b.end ( ) , mod_lesser );

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

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

   // To test for inclusion under an asscending order
   // with the default binary predicate less <int> ( )
   bool Result1;
   Result1 = includes ( v1a.begin ( ) , v1a.end ( ) ,
      v1b.begin ( ) , v1b.end ( ) );
   if ( Result1 )
      cout << "All the elements in vector v1b are "
           << "contained in vector v1a." << endl;
   else
      cout << "At least one of the elements in vector v1b "
           << "is not contained in vector v1a." << endl;

   // To test for inclusion under descending
   // order specify binary predicate greater<int>( )
   bool Result2;
   Result2 = includes ( v2a.begin ( ) , v2a.end ( ) ,
      v2b.begin ( ) , v2b.end ( ) , greater <int> ( ) );
   if ( Result2 )
      cout << "All the elements in vector v2b are "
           << "contained in vector v2a." << endl;
   else
      cout << "At least one of the elements in vector v2b "
           << "is not contained in vector v2a." << endl;

   // To test for inclusion under a user
   // defined binary predicate mod_lesser
   bool Result3;
   Result3 = includes ( v3a.begin ( ) , v3a.end ( ) ,
      v3b.begin ( ) , v3b.end ( ) , mod_lesser );
   if ( Result3 )
      cout << "All the elements in vector v3b are "
           << "contained under mod_lesser in vector v3a."
           << endl;
   else
      cout << "At least one of the elements in vector v3b is "
           << " not contained under mod_lesser in vector v3a." 
           << endl;
}
  
  
  
  
  
  
  
  
  

需求

標題: <algorithm>

命名空間: std

請參閱

參考

includes (STL Samples)

Predicate Version of includes

標準樣板程式庫