Solution to Written Part of HW2

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

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

View Full Document Right Arrow Icon
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' ; }
Background image of page 1

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

View Full Document Right Arrow Icon
}; 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());
Background image of page 2
Image of page 3
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 Right Arrow Icon
Ask a homework question - tutors are online