template< class T, class Compare> inline const T& max( const T& a, const T& b, Compare comp) { return comp(a, b) ? b : a; }
template< class T, class Compare> inline const T& min( const T& a, const T& b, Compare comp) { return comp(b, a) ? b : a; }
使用方式如下:
bool int_less( int a, int b) { return a < b; } int main() { int a = 10; int b = 20; int result; result = max(a, b, int_less); cout << "max(a, b): " << result << endl; result = min(a, b, int_less); cout << "min(a, b): " << result << endl; return 0; }