#include #include #include #include /* * Numeric template parameters are useful to write a function which can * apply to arrays of any size. */ template T arrmax(const std::array &arr) { if(N < 1) throw std::underflow_error("Taking max of empty array"); auto scan = arr.begin(); T ret = *scan++; while(scan < arr.end()) { if(ret < *scan) { ret = *scan; } ++scan; } return ret; } int main() { std::array a1 = { 4, 99, -1, 45, -6, 11, 22, 3, -11, 9 }; std::array a2 = { -10, -5, -3, -8, -17 }; std::array a3 = { "This", "is", "a", "very", "short", "sentence" }; std::array a4 = { 20.1, -3.3, 1661.7, 2.2 }; std::cout << arrmax(a1) << " " << arrmax(a2) << " " << arrmax(a3) << " " << arrmax(a4) << std::endl; }