COMP2004
Programming Practice
2002 Summer School


Kevin Pulo
School of Information Technologies
University of Sydney


(page 1)


Generic Code




(page 2)


Finding an int in a vector


vector::size_type
find(const vector &v,
const int &value) {
for (vector::size_type i = 0;
i < v.size(); ++i)
if (v[i] == value)
return i;
return v.size();
}


(page 3)


Finding a string in a vector


vector::size_type
find(const vector &v,
const string &value) {
for (vector::size_type i = 0;
i < v.size(); ++i)
if (v[i] == value)
return i;
return v.size();
}


(page 4)


Silly to write it twice




(page 5)


Template Functions


template
typename vector::size_type
find(const vector &v,
const T &value)
{
for (typename vector::size_type
i = 0; i < v.size(); ++i)
if (v[i] == value)
return i;
return v.size();
}


(page 6)


Typename...




(page 7)


Calling Template Functions




(page 8)


Another Template Function


template
T sum(const vector &v)
{
T result = 0;
for (typename vector::size_type
i = 0; i < v.size(); ++i)
result += v[i];
return result;
}


(page 9)


Using the Template


int main() {
vector v;
v.push_back(1);
v.push_back(10);
v.push_back(5);
cout << sum(v) << endl;
}


(page 10)


Using the Template II


int main() {
vector< vector > v;
// fill v with data
cout << sum(v) << endl;
}


(page 11)


Common Behaviour




(page 12)


Using the Template III


int main() {
vector v;
v.push_back("1");
v.push_back("10");
v.push_back("5");
cout << sum(v) << endl;
}


(page 13)


A Real Problem


T result = 0;


(page 14)


Sequential Access




(page 15)


Find with Iterators


template
typename vector::const_iterator
find(const vector &v,
const T &value)
{
typename vector::const_iterator
b = v.begin(), e = v.end();
while ( (b != e) && (*b != value) )
++b;
return b;
}


(page 16)


What About Lists?


template
typename C::const_iterator
find(const C &v, const T &value)
{ ... }
  • However, this isn't allowed by C++
  • So we need rewrite it for list
  • But code repetition is bad...


(page 17)


Iterators




(page 18)


The Final find()


template
It find(It begin, It end, const T &value) {
while ( (begin != end) &&
(*begin != value) )
++begin;
return begin;
}


(page 19)


Using find()







(page 20)


Iterator functions





(page 21)


What about classes?



template
class Node {
T value;
Node *next;
...
};


(page 22)


Template classes


template
class List {
Node *head;
...
};

template
void List::insert_at_front(T val) {
head = new Node(val, head);
}


(page 23)


Using template classes



List li;
li.insert_at_front(3);
li.insert_at_front(42);


List ls;
ls.insert_at_front("3");
ls.insert_at_front("42");


(page 24)


Nested template classes


template
class List {
class Node {
T value;
Node *next;
...
};
Node *head;
...
};
  • Previously:Node ni;
  • Now:List::Node ni;


(page 25)


Template code


template
T add(T t1, T t2) {
T result = t1 + t2;
return result;
}
  • Now you do
int i = 3, j = 9;
cout << add(i, j) << endl;


(page 26)


Instantiating templates


int add(int t1, int t2) {
int result = t1 + t2;
return result;
}


(page 27)


Defining templated functions


int add(int t1, int t2);
template
T add(T t1, T t2) {
T result = t1 + t2;
return result;
}
  • Because the function code is needed for instantiation later


(page 28)


Template definitions














































































































































(page 29)