Varidic Template(三)
迭代器的递归调用实现max功能,max函数接受任意参数
#include<iostream> using namespace std; struct _Iter_less_iter { template<typename _Iterator1 , typename _Iterator2> bool operator()(_Iterator1 _it1, _Iterator2 _it2) const { return *_it1 < *_it2; } }; template<typename _ForwardIterator> inline _ForwardIterator self_max_element(_ForwardIterator _first, _ForwardIterator _last) { return self_max_element(_first, _last, _Iter_less_iter()); } template<typename _ForwardIterator, typename _Compare> _ForwardIterator self_max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) { if (__first == __last) return __first; _ForwardIterator __result = __first; while (++__first != __last) { if (__comp(__result, __first)) __result = __first; } return __result; } template<typename _Tp> inline _Tp self_max(initializer_list<_Tp> _l) { return *self_max_element(_l.begin(), _l.end()); } int main() { cout << self_max({57,48,60,100,20,18}) << endl; return 0; }