CPP   22

max 2 cpp

Guest on 5th July 2022 05:10:55 PM

  1. // max.cpp
  2. #include<iostream>
  3. using namespace std;
  4.  
  5.  
  6. // max(): maximum of two values of any type
  7. template<typename T>
  8. T const& max (T const& a, T const& b)
  9. {
  10.    return  a < b ? b : a;
  11. }
  12.  
  13. // max(): maximum of three values of any type
  14. template <typename T>
  15. T const& max (T const& a, T const& b, T const& c)
  16. {
  17.    return ::max (::max(a,b), c);
  18. }
  19.  
  20. int main()
  21. {
  22.    cout << ::max(7, 42, 68)     << endl;  // calls 3 argument version
  23.    cout << ::max(7.0, 42.0)     << endl;  // calls max<double>
  24.    cout << ::max('a', 'b')      << endl;  // calls max<char>
  25.    cout << ::max(7, 42)         << endl;  // calls max<int>
  26.    cout << ::max<double>(7, 42) << endl;  // calls max<double>
  27.  
  28.    return 0;
  29. }
  30.  
  31.  
  32. // output:
  33. //
  34. // 68
  35. // 42
  36. // b
  37. // 42
  38. // 42

Raw Paste


Login or Register to edit or fork this paste. It's free.