lecture9

lecture9 - Monday,May1 st Templates STLIterators

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

View Full Document Right Arrow Icon
Monday, May 1 st   Templates STL Iterators STL Algorithms (find and find_if) Your midterm is next Monday! For more recursion review (hint!), go to: http://www.sparknotes.com/cs/recursion/examples/summary.html
Background image of page 1

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

View Full DocumentRight Arrow Icon
Recursion Challenge The following function iteratively computes x n int exponentiate(int x, int n) { int i, result = 1; for(i=0; i<n; i++) result *= x; return result; } Write a function to do this recursively.  For a bigger challenge: Use the knowledge that x n  = = (x 2 ) (n/2)  when n is even to write a more efficient solution  to the above problem  
Background image of page 2
Templates IDEA:  C++ templates  allow us to write a  function  or  class  once and use it  with many different data types:  main() { circle a(5), b(6); elephant e(10000), f(7500); int n = 100, p = 101; circle biggest_circ = max (a,b); elephant biggest_el = max (e,f); int biggest_int = max (n,p); } Wouldn’t it be nice if we  could write the max   function  once  and have it  work for  all  of the above  data types?  We can!! With templates.  
Background image of page 3

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

View Full DocumentRight Arrow Icon
Templates How to do this  without  templates: int max(int a, int b) { if (a > b) return(a); else return(b); } point max(point a, point b) { if (a > b) return(a); else return(b); } elephant max(elephant a, elephant b) { if (a > b) return(a); else return(b); } ... It’s  ugly  – we have to write  a new version of the  function for each data type  it operates on…
Background image of page 4
Templates // this doesn't quite do it either typedef int item; item max(item a, item b) { if (a > b) return(a); else return(b); } Why isn’t typedef  sufficient?  Answer:  Because we can only  typedef  a single data type. In the above  example, I chose  int .  But now I  can’t  use my max function on  elephants  or  points ! int int typedef int item; { if (a > b) return(a); else return(b); }
Background image of page 5

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

View Full DocumentRight Arrow Icon
Templates Here’s how to do it right, with  C++ templates : Add the following line above your  function definition: template <class  xxx > Then use  xxx  as our data type  throughout our function: xxx  max( xxx  a,  xxx  b) int max(int a, int b) { if (a > b) return(a); else return(b); } // using our template. .. main() { int a = max(5,6); point p(1,1), q(2,2); point r = max(p,q); ... } template <class  Item > Item Item Item
Background image of page 6
Templates Here’s another example:   A generalized swap function! SWAP.H template <class Data> void swap( Data &x, Data &y) { Data temp; temp = x; x = y; y = temp; } TEST.CPP #include <string> #include “swap.h” main() { point a(1,1), b(5,5); swap(a,b); int p=-1, q=-2; swap(p,q); string x("Carey"), y("Larry"); swap(x,y); cout << x; } Note:  You can even define local  variables of the templated type in  your function!    
Background image of page 7

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

View Full DocumentRight Arrow Icon
Function Template Details 1. A template function should be defined at the top of the CPP file  where it is used. ..
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/20/2009 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 41

lecture9 - Monday,May1 st Templates STLIterators

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online