ࡱ> Z[  !"#$%&'()*+,-./0123456789:;<=>?@ABCDEGHIJKLMNOPQRSTUVWXY\RdO)Y" PicturesPowerPoint Document(o|SummaryInformation(F'  !"#$%&'()*+,-nx!;B R2pMc³ SCPNG  IHDR.gAMAܲ pHYs  IDATxAJPET:q.}z.AGLA*mC$3y$䯎?a7@d=v~3 l,IIIIIII=:nwLu5͋NCLj}(1I1I1I1I1I1I1I1IX'L:_ y0I9IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIzpǂ]=aRݰ=p1I1I1I1I1I1I1I_'P;IENDB`n2G[]wYVï^>{O#߫iPNG  IHDRy4gAMAܲ pHYs  yIDATxA`D< #t% Sнi]+&}?<>#y˿I˟/an:?LwgF`k$ELR$ELR$ELR$ELR$ELR$ELR$ELR$Elylwr:s&fʯ=(1I1I1I1I1I1I1I1I[ }c~0zªpdžzqU9IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIno>;6'j~?=ލv8IIIIIIIIIIII-s0'\r:л[~+{xIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFּ??n;0zs1I1I1I1I1I1I1I1RIENDB` ՜.+,D՜.+,<    'On-screen Showcn-s r Times New RomanArial Wingdi7(   K6"Wagner@cs.ucr.eduN:http://www.cs.ucr.edu/~wagnerH4http://www.cs.ucr.edu/cs12/ 00DTimes New RomanZ '; DArialNew RomanZ '; " DWingdingsRomanZ '; 0DProFontsRomanZ ';  b .  @n?" dd@  @@`` xp v        /   *,-./01234567<$۰$۰$۰$۰$۰$۰$۰$۰$۰$۰$۰b$R2pMc³ SC۰b$Vï^>{O#߫i۰c $ n3f3@8d sʚ;+4ʚ;g4]d]d@t@0pp <4!d!d,gX<4dddd,gXg4\d\d4@@8p@ pp>___PPT9 h___PPT2001D<4X?- 2003 WL Truppel \CS 12: Intro. Computer Science II " Lecture 12O =! TemplatesWagner Truppel Lecturer, Dept. of Computer Science & Engineering UC Riverside wagner@cs.ucr.edu http://www.cs.ucr.edu/~wagner http://www.cs.ucr.edu/cs12O  O`a~V A simple puzzle& Say you have a glass of orange juice and another glass, of tomato juice. Now suppose that for some reason, you want to swap the two. How do you do it?W A simple puzzle& Say you have a glass of orange juice and another glass, of tomato juice. Now suppose that for some reason, you want to swap the two. How do you do it? You need another glass !$ UAnother puzzle& THow do you swap two integers ? The integers are stored in two containers (variables)UUXA general swap function^We d like to be able to say: Gimme two values of some type T and I ll swap them with this function void swap(T& var1, T& var2) { T temp = var1; var1 = var2; var2 = temp; } eZZ)ZZZ<(  (  Y A general binary search functionRemember our friend the binary search algorithm? We had it for objects of class Book int BinS(Book& target, Book a[], int lo, int hi) { int mid = (lo + hi) / 2; if (target == a[mid]) return mid; // found it ! else if (target == a[hi]) return hi; // found it ! else if (mid == lo) return -1; // book not found else if (target < a[mid]) return BinS(lo, mid); // search lower half else // target > a[mid]) return BinS(mid, hi); // search upper half } But the binary search algorithm is more general than thatUZ3B:ZP      B  :  U+5.79\Z A general binary search function"The binary search algorithm is very general& it applies to any type supporting the operations of comparison for equality (==) and for ordering (<, <=, >=, or >) int BinS(T& target, T a[], int lo, int hi) { int mid = (lo + hi) / 2; if (target == a[mid]) return mid; // found it ! else if (target == a[hi]) return hi; // found it ! else if (mid == lo) return -1; // book not found else if (target < a[mid]) return BinS(lo, mid); // search lower half else // target > a[mid]) return BinS(mid, hi); // search upper half }-B;c      B  tA"\No problem, right ?So, why can t we simply write this: void swap(T& var1, T& var2) { T temp = var1; var1 = var2; var2 = temp; }$ZZ)ZZ$  (] No problem, right ?We need to tell the compiler that T is a parameter and not an actual type. How? template void swap(T& var1, T& var2) { T temp = var1; var1 = var2; var2 = temp; } Despite the keyword class, you can now use this function for any type that supports the assignment operator. swap(int1, int2); // swap int vars swap(char1, char2); // swap char vars swap(foo1, foo2); // swap Foo vars ZQZ0Z)ZZZ"   (      $    +      m  H ! ^ Cranky Compilers>Many compilers don t like templates very much Make sure to put your template function definitions in the same file where they re used, and before they re used$_ Class templatesdWe ve seen function templates (also referred to as template functions) We can also have class templates (never referred to as template classes) Same basic idea, same basic syntax` Class templates7template class Pair { private: T first; T second; public: Pair(T frst, T sec); T getFirst() const; } template Pair::Pair(T frst, T sec) { first = frst; second = sec; } Inside the class, just use T as if it was some specific type (class or not) Member functions are then template functionsZ ZZZ)ZSZyZ                &]PJ /a Class templates%template class Pair { private: T first; T second; public: Pair(T frst, T sec); T getFirst() const; } How to use it? Just as before, but now you need to specify what type to use: Pair point(2, 7); // a two dimensional point Pair glasses(Lens(), Lens()); // a pair of lensesNZ ZZZ)ZZZ                T-.>J _b/PsxHH@Rg(HH(dh   ` www3ff` 3fff` ___>?" dF@,?n<d@uA @ " d`  n?" dd@   @@``PR   @ ` `P0p>> - 2003 WL Truppel \CS 12: Intro. Computer Science II " Lecture 12 X( = 2  NA?"! :    NBgֳgֳ ?"`  T Click to edit Master title style! !<  H>gֳgֳ ?"  RClick to edit Master text styles Second level Third level Fourth level Fifth level!     S  N0?gֳgֳ ?"`p  Z*    N?gֳgֳ ?"`    \*     NP@gֳgֳ ?"`@  \*   N  6n޽h? ? www3ff $Blank Presentation3  - 2003 WL Truppel \CS 12: Intro. Computer Science II " Lecture 12 +#  ( |  B  N1?"442  NpD?"! :    N<gֳgֳ ?"` D T Click to edit Master title style! !  H9gֳgֳ ?"P  D W#Click to edit Master subtitle style$ $  Np>gֳgֳ ?"`p D Z*    NZ0gֳgֳ ?"`   D \*     N@W0gֳgֳ ?"`@ D \*   N  6n޽h? ?  www3ff `<f(  < < 01Z "P   ה R*   < 00 "   ה T*  r < c $ ?"  ה$ < 0P " @ ה RClick to edit Master text styles Second level Third level Fourth level Fifth level!     S < 6 "`P  ה R*   < 60 "`  ה T*  H < 0޽h ? ̙33@ pD( @ $  D D 0P "P   ה BCS 12 Lecture 12  D 0T[ "   ה T*   D 6@W[ "`P  ה BCS 12 Lecture 12  D 6p; "`  ה T*  H D 0޽h ? ̙33  @ L(  P   c $Z "0P D   c $@Z "P  D B  s *޽h ? 3ff(   P(  r  S @Z@  D r  S Op0 D $  C A fig1.pdf 00132CB4Earth B9B583FF:`H  0޽h ? www3ff~  V ( 0K4 r  S [@  D r  S `^[p  D $  C A  fig1b.pdf 00132CB4Earth B9B583FF:` N  HV/gֳgֳ ? 0,$D  Now& does it matter that the glasses had orange juice and tomato juice?H  0޽h ? www3ffG  V o(  r  S @@  D   S @p<$  D   H 3gֳgֳ ? ,$D Nvoid swap(int& var1, int& var2) { int temp = var1; var1 = var2; var2 = temp; }X"+"#>  */  H\/gֳgֳ ?  ,$D  SDoes it matter that you have two integers as opposed to two values of another type?TT$  HU/gֳgֳ ?p,$D x2But you need a third one& *H  0޽h ? www3ff  W ( 00// l  C 0:`  D l  C L  D H  0޽h ? www3ff  X $( 00// r  S pp2`  D r  S u2`0 D H  0޽h ? www3ff  Y $( 1111 r  S _\`  D r  S \\`0 D H  0޽h ? www3ffU  Z  }( 00// l  C r[`  D l  C q[  D ]  Hu[gֳgֳ ? ,$D _Because T would be interpreted as the name of some class we defined, rather than as a parameter6`M H  0޽h ? www3ff  \ RJ0(  l  C `U\@  D l  C @T\0 D   HW\gֳgֳ ?@ JFunction templatesZnH  0޽h ? www3ff  ] @(  l  C n[`  D l  C P\ D H  0޽h ? www3ff  ^ P( 00// l  C S\`  D l  C  M\ D H  0޽h ? www3ff  _ `$( | r  S ^\p  D r  S V\p D H  0޽h ? www3ff  ` p$( | r  S  3p  D r  S  3p D H  0޽h ? www3ff tlPH(  HR H 3 <   4r H # < @  4  H H 0޽h ? ̙33dxp^RЀ3ÿ lHbP  @AL G@;b `B&V=fn7X2n1 &bQ>n5Xn nY2nn ^6nQ, ng-n1 &bg,[nց~ pt#=\P}aBgP$ st#=\P}aBgP$?9fnX2n;9E#gQCn7^n nY(nm"^6L*V ng,n;9Egnd?"mnց~8t^8M=}}A^8M A"8t^8M=}}A^8M A"@Gmn8X2n[(Z!mQCn7^n nYF6E^6;JWnjU[(Z!mCG`l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~/˽nnnnnnnnnnnnnnnnnnnnn~| / ˾nnnnnnnnnnnnnn nnnnnnnnnnnnnn~_/}˿nnnnnnnnnnnnnnnnnn+~B0fnn(~'0Pnn~0=n~0on~0wn~͆0׆~n~1Ćn~1n~1n~1n~s1n~c1n~S1tn~F2kn~<2cn~32[nԁ~)2222222Snnnnnnnnnnnnnn~ 222222222L n nnnnnnnnnnnnnnnnnnnnnnnnnn~22222Dnnnnnnnnnnnnn@~222?nnn~ 2=ņn~ 2:Ɔn~27Ɇn~25Ɇn~2ˆnDocumentSummaryInformation8Current User..ngsProFontBlank Presentation TemplatesA simple puzzleA simple puzzleAnother puzzleA general swap function!A general binary search function!A general binary search functionNo problem, right ?No problem, right ?Cranky CompilersClass templatesClass templatesClass templates  Fonts UsedDesign Template Slide Titles  8@ _PID_HLINKS'AHmailto:Wagner@cs.ucr.eduhttp://www.cs.ucr.edu/~wagnerhttp://www.cs.ucr.edu/cs12&_K|CTWagner Truppel