Solution to Written Part of HW2

Solution to Written Part of HW2 - Solution to Written Part...

This preview shows pages 1–3. Sign up to view the full content.

Solution to Written Part of HW2 1. a. template < class C, class Tester, class Transform> void Modify_If(C & a, Tester f, Transform g) { if (f(a)) g(a); } b. template < class C> class testnum1 { public : bool operator ()(C x) { return (x >= 0 && x <= 100); } }; c. template < class C> class add3 { public : bool operator ()(C & x) { x += 3; } }; d. int b=4; Modify_If(b,testnum1< int >(),add3< int >()); e. class checka { public : bool operator ()(string s) { return (s[0] == 'a' ); } }; f. class adda { public : bool operator ()(string & s) { s += 'a' ; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
}; g. string s = ann; Modify_If(s,checka(),adda()); h. template < class C> class addn { public : addn(C z=0):val(z) {}; bool operator ()(C & x) { x += val; } private : C val; }; i. float y = 1.5; Modify_If(y,testnum1< float >(),addn< float >(4)); 2. // a) sort the elements of V in descending order, then sort them in // ascending order sort(V.begin(), V.end(), greater< int >()); assert(is_sorted(V.begin(), V.end(), greater< int >())); sort(V.begin(),V.end());
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 3

Solution to Written Part of HW2 - Solution to Written Part...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online