STL: equal

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
   );
 
Return Value

true if and only if the ranges are identical or equivalent under the binary predicate when compared element by element; otherwise, false.

 
Remarks

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.

注:如果两个序列在[first,last)区间内相等,equal()返回true。如果第二序列的元素比较多,多出来的元素不予考虑。因此,如果我们希望保证两个序列完全相等,必须先判断其元素个数是否相等。

if( vec1.size() == vec2.size() ) && equal( vec1.begin(), vec1.end(), vec2.begin() ) ){...}

equal的第二个序列中元素个数必须大于等于第一个序列中元素个数。

原文地址:https://www.cnblogs.com/freewater/p/2946746.html