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
    解决输入的迭代器第一个元素的位置在要测试的第一个范围。

  • _Last1
    解决输入的迭代器通过最终元素的位置一在要测试的第一个范围。

  • _First2
    解决输入的迭代器第一个元素的位置在要测试的第二个范围。

  • _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

请参见

参考

标准模板库