List of all available Websheets
Viewing cpp/cs104/functors/max_template_comparator by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | Examine the functions below to find the max of two doubles and two strings. Merge them to a single function template that would work for any type with a '>' operator. |
Remarks Comments, history, license, etc. | Copied from problem cpp/cs104/templates/func_template_max (author: redekopp@usc.edu) |
Engine | |
Template / Reference solution |
using namespace std; ostream& operator<<(ostream& ostr, const vector<int>& v1){ for(vector<int>::const_iterator it = v1.begin(); it != v1.end(); ++it){ cout << *it << " "; } cout << endl; return ostr; } \[ REDACTED \show: template <typename T> T mymax(const T& a, const T& b) { if(a > b) return a; return b; } ]\ struct SizeComp { bool operator()(const vector<int>& a, const vector<int>& b) const { \[ REDACTED ]\ } }; struct SumComp { bool operator()(const vector<int>& a, const vector<int>& b) const { \[ REDACTED ]\ } }; int main() { int data1[] = {10, 20, 30}; int data2[] = {1, 2, 3, 4, 5}; vector<int> v1(data1, data1+3); vector<int> v2(data2, data2+5); cout << "max of 4.3 and 6.1 is " << mymax(4.3, 6.1, greater<double>()) << endl; cout << "max of v1 and v2 is " << mymax(v1, v2, SizeComp()) << endl; cout << "max of v1 and v2 is " << mymax(v1, v2, SumComp()) << endl; return 0; } |
C++ test suite json list of stdin/args tests e.g. [{"stdin":"hi", "args":["4", "5"]}, {"stdin":"noargs"}] to just run once with no input use [{}] | [{}] |
Solution visibility |
Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.