#include #include class bad_thing: public std::exception{ const char* msg; virtual const char* what() const throw() { return msg; } public: bad_thing(const char* _msg) : msg(_msg) {} }; // Function to find the maximum element of a list. // The list may contain one or more elements. // We throw an exception if there are zero elements. // template Tp max(const std::list& arg) { using namespace std; if (!arg.size()) throw bad_thing("max of empty list"); typename list::const_iterator ii(arg.begin()); // Initialize the result to the first element. // This is well known to be the only valid way of doing it: Tp rslt(*ii++); while(ii != arg.end()) { rslt = max(rslt, *ii++); } return rslt; }