equal

由項目實際上會比較兩個範圍項目的一個二進位述詞或等於指定的是否相等。

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

參數

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

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

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

  • _Comp
    定義要滿足條件的使用者定義之述詞函式物件,如果兩個項目將會視為相等。一個二進位述詞採用兩個引數並傳回 true ,當內容和 false ,則內容。

傳回值

true ,則為;如果,只有在範圍相同或相當於這個二進位述詞下進行比較時,項目會以項目,否則, false

備註

要搜尋的範圍必須是有效的,任何指標必須 dereferenceable,而最後一個位置開始可取得的會增加。

演算法的時間複雜度是線性在範圍中的元素數。

operator== 用來判斷在項目之間的相等必須強制在其運算元之間的一個層級的關聯性。

範例

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

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

int main( )
{
   using namespace std;
   vector <int> v1, v2, v3;
   vector <int>::iterator Iter1, Iter2, Iter3;

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

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

   int iii;
   for ( iii = 0 ; iii <= 5 ; iii++ )
   {
      v3.push_back( 10 * iii );
   }
   
   cout << "v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")" << endl;

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

   cout << "v3 = ( " ;
   for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
      cout << *Iter3 << " ";
   cout << ")" << endl;

   // Testing v1 and v2 for equality under identity
   bool b;
   b = equal( v1.begin( ), v1.end( ), v2.begin( ) );

   if ( b )
      cout << "The vectors v1 and v2 are equal under equality."
           << endl;
   else
      cout << "The vectors v1 and v2 are not equal under equality."
           << endl;

   // Testing v1 and v3 for equality under identity
   bool c;
   c = equal( v1.begin( ), v1.end( ), v3.begin( ) );

   if ( c )
      cout << "The vectors v1 and v3 are equal under equality."
           << endl;
   else
      cout << "The vectors v1 and v3 are not equal under equality."
           << endl;

   // Testing v1 and v3 for equality under twice
   bool d;
   d = equal( v1.begin( ), v1.end( ), v3.begin( ), twice );

   if ( d )
      cout << "The vectors v1 and v3 are equal under twice."
           << endl;
   else
      cout << "The vectors v1 and v3 are not equal under twice."
           << endl;
}
  
  
  

需求

標題: <algorithm>

命名空間: std

請參閱

參考

標準樣板程式庫