泛型算法系列23:includes();

#include #include #include #include using namespace std; /************************************************************************/ /* */ template inline bool my_Includes(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2) { // test if all [_First1, _Last1) in [_First2, _Last2), using operator< _DEBUG_ORDER(_First1, _Last1); _DEBUG_ORDER(_First2, _Last2); for (; _First1 != _Last1 && _First2 != _Last2; ) if (_DEBUG_LT(*_First2, *_First1)) return (false); else if (*_First1 < *_First2) ++_First1; else ++_First1, ++_First2; return (_First2 == _Last2); } /************************************************************************/ template inline bool my_Includes(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _Pr _Pred) { // test if set [_First1, _Last1) in [_First2, _Last2), using _Pred _DEBUG_ORDER_PRED(_First1, _Last1, _Pred); _DEBUG_ORDER_PRED(_First2, _Last2, _Pred); for (; _First1 != _Last1 && _First2 != _Last2; ) if (_DEBUG_LT_PRED(_Pred, *_First2, *_First1))//_Pred(x,y) return (false); else if (_Pred(*_First1, *_First2)) ++_First1; else ++_First1, ++_First2; return (_First2 == _Last2); } /************************************************************************/ int main() { int ia1[] = {13,1,21,2,0,34,5,1,8,3,21,34}; int ia2[] = {21,2,8,3,5,1}; sort(ia1, ia1 + 12); sort(ia2, ia2 + 6); bool ret = includes(ia1, ia1 + 12, ia2, ia2 + 6); cout << "every element of ia2 contained in ia1?" << (ret ? "true" : "false" ) << endl; vector ivect1(ia1, ia1 + 12); vector ivect2(ia2, ia2 + 6); sort(ivect1.begin(), ivect1.end(), greater()); sort(ivect2.begin(), ivect2.end(), greater()); ret = includes(ivect1.begin(),ivect1.end(), ivect2.begin(),ivect2.end(), greater()); cout << "every element of ia2 contained in ia1?" << (ret ? "true" : "false" ) << endl; return 0 ; } /************************************************************************/ /* template inline bool __CLRCALL_OR_CDECL _Debug_lt_pred(_Pr _Pred, _Ty1& _Left, _Ty2& _Right, const wchar_t *_Where, unsigned int _Line) { // test if _Pred(_Left, _Right) and _Pred is strict weak ordering if (!_Pred(_Left, _Right)) return (false); else if (_Pred(_Right, _Left)) _DEBUG_ERROR2("invalid operator<", _Where, _Line); return (true); } */ /************************************************************************/

 

注:必须传入已排序的容器

你可能感兴趣的:(C++,STL)