/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Champlain College SDEV-340-81 * * C++ Week 10: Assignment (second semester) - Starting out with C++ Early Objects (9 Edition) (2020/07/19) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Exceptions, Templates, and the Standard Template Library (STL) * Templates - Chapter 16 (Page 1034) * Programming Challenge (3. Min/Max Templates) * Templates that returns the min and max of two arguments * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Written by Llewellyn van der Merwe , July 2020 * Copyright (C) 2020. All Rights Reserved * License GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include #include using namespace std; // compare values template bool comp(const T &a, const T &b) { return (a < b); } // compare string length template<> bool comp(const string &a, const string &b) { return (a.length() < b.length()); } // Get the Largest entity // http://www.cplusplus.com/reference/algorithm/max/ template const T &Max(const T &a, const T &b) { return comp(a, b) ? b : a; } // Get the Smallest entity // http://www.cplusplus.com/reference/algorithm/min/ template const T &Min(const T &a, const T &b) { return comp(a, b) ? a : b; } int main() { // The Test Values int i[] = {7, 5}; float f[] = {3.1f, 3.2f}; double d[] = {3.64, 3.232}; string s[] = {"My Longer Name", "My short Name"}; // Demonstrate the Max & Min template cout << "Between (" << *i << ") and (" << *(i + 1) << ") MAX=" << Max(*i, *(i + 1)) << " MIN=" << Min(*(i + 0), *(i + 1)) << endl; cout << "Between (" << *f << ") and (" << *(f + 1) << ") MAX=" << Max(*f, *(f + 1)) << " MIN=" << Min(*(f + 0), *(f + 1)) << endl; cout << "Between (" << *d << ") and (" << *(d + 1) << ") MAX=" << Max(*d, *(d + 1)) << " MIN=" << Min(*(d + 0), *(d + 1))<< endl; cout << "Between (" << *s << ") and (" << *(s + 1) << ") MAX=" << Max(*s, *(s + 1)) << " MIN=" << Min(*s, *(s + 1)) << endl; return 0; }