equal
Compares two ranges element by element either for equality or equivalence in a sense specified by a binary predicate.
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
An input iterator addressing the position of the first element in the first range to be tested._Last1
An input iterator addressing the position one past the final element in the first range to be tested._First2
An input iterator addressing the position of the first element in the second range to be tested._Comp
User-defined predicate function object that defines the condition to be satisfied if two elements are to be taken as equivalent. 이진 조건자는 두 개의 인수를 사용하고 만족할 때는 true를 만족하지 못할 때는 false를 반환합니다.
반환 값
true if and only if the ranges are identical or equivalent under the binary predicate when compared element by element; otherwise, false.
설명
The range to be searched must be valid; all pointers must be dereferenceable and the last position is reachable from the first by incrementation.
The time complexity of the algorithm is linear in the number of elements contained in the range.
The operator== used to determine the equality between elements must impose an equivalence relation between its operands.
예제
// 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