1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
#include <string>
template < typename T > T minMax( T a, T b, std::string what ) ;
int main()
{
minMax( 1, 5, "min" );
minMax( std::string("Hello"), std::string("Dolly"), "max");
minMax( '9', 'z', "min" );
}
template < typename T > const T& minv( const T& a, const T& b )
{ return a<b ? a : b ; }
template < typename T > const T& maxv( const T& a, const T& b )
{ return a<b ? b : a ; }
template < typename T > const T& minmaxv( const T& a, const T& b, const std::string& what )
{
if( what == "min" ) return minv(a,b) ;
else return maxv( a, b ) ;
}
template < typename T > T minMax( T a, T b, std::string what )
{
const T& result = minmaxv( a, b, what ) ;
if( what == "min" ) std::cout << "the minimum of " << a << " and " << b << " is: " << result << '\n' ;
else if( what == "max" ) std::cout << "the maximum of " << a << " and " << b << " is: " << result << '\n' ;
else std::cerr << "error: neither 'min' nor 'max'\n" ;
return result ;
}
|