#include <algorithm> #include <iostream> #include <vector> using namespace std; /************************************************************************/ /* max() && min() */ /************************************************************************/ template<class _Ty> inline const _Ty& (__CLRCALL_OR_CDECL my_max)(const _Ty& _Left, const _Ty& _Right) { // return larger of _Left and _Right return (_DEBUG_LT(_Left, _Right) ? _Right : _Left); } template<class _Ty, class _Pr> inline const _Ty& (__CLRCALL_OR_CDECL my_max)(const _Ty& _Left, const _Ty& _Right, _Pr _Pred) { // return larger of _Left and _Right using _Pred return (_DEBUG_LT_PRED(_Pred, _Left, _Right) ? _Right : _Left); } /************************************************************************/ /*template<class _Ty1, class _Ty2> inline bool __CLRCALL_OR_CDECL _Debug_lt(const _Ty1& _Left, const _Ty2& _Right, const wchar_t *_Where, unsigned int _Line) { // test if _Left < _Right and operator< is strict weak ordering if (!(_Left < _Right)) return (false); else if (_Right < _Left) _DEBUG_ERROR2("invalid operator<", _Where, _Line); return (true); }*/ /************************************************************************/ /* min_element() && max_element() */ /************************************************************************/ //_DEBUG_LT(x, y) ((x) < (y)) template<class _FwdIt> inline _FwdIt my_min_element(_FwdIt _First, _FwdIt _Last) { // find smallest element, using operator< _DEBUG_RANGE(_First, _Last); _FwdIt _Found = _First; if (_First != _Last) for (; ++_First != _Last; ) if (_DEBUG_LT(*_First, *_Found)) _Found = _First; return (_Found); } template<class _FwdIt> inline _FwdIt my_max_element(_FwdIt _First, _FwdIt _Last) { // find largest element, using operator< _DEBUG_RANGE(_First, _Last); _FwdIt _Found = _First; if (_First != _Last) for (; ++_First != _Last; ) if (_DEBUG_LT(*_Found, *_First)) _Found = _First; return (_Found); } int main() { int ia[] = {7, 5, 2, 4, 3}; const vector<int> ivec(ia, ia + 5); vector<int>::const_iterator iter; int mval = max(max(max(max(ivec[4],ivec[3]),ivec[2]),ivec[1]),ivec[0]); cout << "the result of invoking max() is:" << mval << endl; mval = my_max(ivec[0],ivec[1]); mval = min(min(min(min(ivec[4],ivec[3]),ivec[2]),ivec[1]),ivec[0]); cout << "the result of invoking min() is:" << mval << endl; iter = max_element(ivec.begin(), ivec.end()); cout << "the result of invoking max_element() is also:" << *iter << endl; iter = min_element(ivec.begin(), ivec.end()); cout << "the result of invoking min_element() is also:" << *iter << endl; return 0; };