一:功能解析
函数原型:
equality (1)
template
bool equal (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2);
predicate (2)
template
bool equal (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, BinaryPredicate pred);
功能:
区间[ first1 , last1 )与以first2开始的区间逐一比较,如果与[ first1 , last1 )全部相等(或者满足pred),返回布尔值真,否则返回假。
例子:
// equal algorithm example
#include // std::cout
#include // std::equal
#include // std::vector
bool mypredicate (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {20,40,60,80,100}; // myints: 20 40 60 80 100
std::vectormyvector (myints,myints+5); // myvector: 20 40 60 80 100
// using default comparison:
if ( std::equal (myvector.begin(), myvector.end(), myints) )
std::cout << "The contents of both sequences are equal.\n";
else
std::cout << "The contents of both sequences differ.\n";
myvector[3]=81; // myvector: 20 40 60 81 100
// using predicate comparison:
if ( std::equal (myvector.begin(), myvector.end(), myints, mypredicate) )
std::cout << "The contents of both sequences are equal.\n";
else
std::cout << "The contents of both sequences differ.\n";
return 0;
}
运行如下:
The contents of both sequences are equal.
The contents of both sequence differ.
二:源码剖析
// TEMPLATE FUNCTION equal WITH PRED
template inline
bool _Equal(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _Pr _Pred)
{ // compare [_First1, _Last1) to [_First2, ...) using _Pred
for (; _First1 != _Last1; ++_First1, (void)++_First2)
if (!_Pred(*_First1, *_First2))
return (false);
return (true);
}
inline bool _Equal(const char *_First1, const char *_Last1,
const char *_First2, equal_to<>)
{ // compare [_First1, _Last1) to [_First2, ...), for chars
return (_CSTD memcmp(_First1, _First2, _Last1 - _First1) == 0);
}
inline bool _Equal(const signed char *_First1, const signed char *_Last1,
const signed char *_First2, equal_to<>)
{ // compare [_First1, _Last1) to [_First2, ...), for signed chars
return (_CSTD memcmp(_First1, _First2, _Last1 - _First1) == 0);
}
inline bool _Equal(const unsigned char *_First1, const unsigned char *_Last1,
const unsigned char *_First2, equal_to<>)
{ // compare [_First1, _Last1) to [_First2, ...), for unsigned chars
return (_CSTD memcmp(_First1, _First2, _Last1 - _First1) == 0);
}
inline bool _Equal(const char *_First1, const char *_Last1,
const char *_First2, _Char_traits_eq>)
{ // compare [_First1, _Last1) to [_First2, ...), for chars
return (_CSTD memcmp(_First1, _First2, _Last1 - _First1) == 0);
}
#if _ITERATOR_DEBUG_LEVEL == 0
template inline
bool equal(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _Pr _Pred)
{ // compare [_First1, _Last1) to [_First2, ...) using _Pred
return (_Equal(_Unchecked(_First1), _Unchecked(_Last1),
_Unchecked(_First2), _Pred));
}
#else /* _ITERATOR_DEBUG_LEVEL == 0 */
template inline
bool _Equal2(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _Pr _Pred, true_type)
{ // compare [_First1, _Last1) to [_First2, ...) using _Pred, checked
return (_Equal(_First1, _Last1,
_First2, _Pred));
}
template inline
_SCL_INSECURE_DEPRECATE
bool _Equal2(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _Pr _Pred, false_type)
{ // compare [_First1, _Last1) to [_First2, ...) using _Pred, unchecked
return (_Equal(_First1, _Last1,
_First2, _Pred));
}
template inline
bool equal(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _Pr _Pred)
{ // compare [_First1, _Last1) to [_First2, ...) using _Pred
_DEBUG_RANGE_PTR(_First1, _Last1, _First2);
_DEBUG_POINTER_IF(_First1 != _Last1, _Pred);
return (_Equal2(_Unchecked(_First1), _Unchecked(_Last1),
_First2, _Pred, _Is_checked(_First2)));
}
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template inline
bool equal(_InIt1 _First1, _InIt1 _Last1,
_InTy (&_First2)[_InSize], _Pr _Pred)
{ // compare [_First1, _Last1) to [_First2, ...) using _Pred
return (_STD equal(_First1, _Last1,
_Array_iterator<_InTy, _InSize>(_First2), _Pred));
}
#endif /* _ITERATOR_DEBUG_ARRAY_OVERLOADS */
#endif /* _ITERATOR_DEBUG_LEVEL == 0 */
// TEMPLATE FUNCTION equal
template inline
bool equal(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2)
{ // compare [_First1, _Last1) to [_First2, ...)
return (_STD equal(_First1, _Last1, _First2,
equal_to<>()));
}
#if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template inline
bool equal(_InIt1 _First1, _InIt1 _Last1,
_InTy (&_First2)[_InSize])
{ // compare [_First1, _Last1) to [_First2, ...)
return (_STD equal(_First1, _Last1, _First2,
equal_to<>()));
}
#endif /* _ITERATOR_DEBUG_ARRAY_OVERLOADS */
// TEMPLATE FUNCTION equal WITH TWO RANGES, PRED
template inline
bool _Equal(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _InIt2 _Last2, _Pr _Pred,
input_iterator_tag, input_iterator_tag)
{ // compare [_First1, _Last1) to [_First2, _Last2)
// using _Pred, arbitrary iterators
_DEBUG_POINTER_IF(_First1 != _Last1 && _First2 != _Last2, _Pred);
for (; _First1 != _Last1 && _First2 != _Last2; ++_First1, (void)++_First2)
if (!_Pred(*_First1, *_First2))
return (false);
return (_First1 == _Last1 && _First2 == _Last2);
}
template inline
bool _Equal(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _InIt2 _Last2, _Pr _Pred,
random_access_iterator_tag, random_access_iterator_tag)
{ // compare [_First1, _Last1) to [_First2, _Last2)
// using _Pred, random-access iterators
if (_Last1 - _First1 != _Last2 - _First2)
return (false);
_DEBUG_POINTER_IF(_First1 != _Last1, _Pred);
return (_Equal(_First1, _Last1, _First2, _Pred));
}
template inline
bool equal(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _InIt2 _Last2, _Pr _Pred)
{ // compare [_First1, _Last1) to [_First2, _Last2) using _Pred
_DEBUG_RANGE(_First1, _Last1);
_DEBUG_RANGE(_First2, _Last2);
return (_Equal(_Unchecked(_First1), _Unchecked(_Last1),
_Unchecked(_First2), _Unchecked(_Last2), _Pred,
_Iter_cat(_First1), _Iter_cat(_First2)));
}
// TEMPLATE FUNCTION equal WITH TWO RANGES
template inline
bool equal(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _InIt2 _Last2)
{ // compare [_First1, _Last1) to [_First2, _Last2)
return (_STD equal(_First1, _Last1, _First2, _Last2,
equal_to<>()));
}
源码摘抄自Visual Studio 2015安装目录algorithm文件中。
点击进入目录----> C++源码剖析目录