Lec12. Templates
Lec12. Templates
Templates
16-4
//Program to demonstrate a function template.
#include <iostream>
using namespace std;
//Interchanges the values of variable1 and variable2.
//The assignment operator must work for the type T.
template<class T>
void swapValues(T& variable1, T& variable2){
T temp;
temp = variable1;
variable1 = variable2;
variable2 = temp;
}
int main( ){
int integer1 = 1, integer2 = 2;
cout << "Original integer values are " << integer1 << " " << integer2 <<
endl;
swapValues(integer1, integer2);
cout << "Swapped integer values are "<< integer1 << " " << integer2 <<
endl;
char symbol1 = 'A', symbol2 = 'B';
cout << "Original character values are: "
<< symbol1 << " " << symbol2 << endl;
swapValues(symbol1, symbol2);
cout << "Swapped character values are: "<< symbol1 << " " << symbol2 <<
endl; Original integer values are: 1 2
return 0;
Swapped integer values are: 2 1
}
Original character values are: A B 16-5
Swapped character values are: B A
Another Function Template
• Declaration/prototype: Template<class T>
void showStuff(int stuff1, T stuff2, T stuff3);
16-6
Algorithm Abstraction
• Refers to implementing templates
• Express algorithms in "general" way:
– Algorithm applies to variables of any type
– Ignore incidental detail
– Concentrate on substantive parts of algorithm
• Function templates are one way C++ supports algorithm abstraction
Compiler Complications
• Have function declarations and definitions separate
• Place template function definition in file where invoked
– Many compilers require it appear 1st
– Often we #include all template definitions
16-7
//Demonstrates a template function that implements
//a generic version of the selection sort algorithm.
#include <iostream> Algorithm
using std::cout;
using std::endl;
abstraction
template<class T> example
void sort(T a[], int numberUsed);
//Precondition: numberUsed <= declared size of the array a. sort.cpp
//The array elements a[0] through a[numberUsed - 1] have values.
//The assignment and < operator work for values of type T.
//Postcondition: The values of a[0] through a[numberUsed - 1] have
//been rearranged so that a[0] <= a[1] <=... <= a[numberUsed - 1].
template<class T>
void swapValues(T& variable1, T& variable2);
//Interchanges the values of variable1 and variable2.
//The assignment operator must work correctly for the type T.
template<class T>
int indexOfSmallest( const T a[], int startIndex, int numberUsed);
//Precondition: 0 <= startIndex < numberUsed. Array elements have values.
//The assignment and < operator work for values of type T.
//Returns the index i such that a[i] is the smallest of the values
//a[startIndex], a[startIndex + 1],..., a[numberUsed - 1].
16-8
#include "sort.cpp"
int main( ){ Algorithm
int i, a[10] = {9, 8, 7, 6, 5, 1, 2, 3, 0, 4};
cout << "Unsorted integers:\n"; abstraction
for (i = 0; i < 10; i++) cout << a[i] << " ";
cout << endl;
example
sort(a, 10); sort.cpp
cout << "In sorted order the integers are:\n";
for (i = 0; i < 10; i++) cout << a[i] << " ";
cout << endl;
double b[5] = {5.5, 4.4, 1.1, 3.3, 2.2};
cout << "Unsorted doubles:\n";
for (i = 0; i < 5; i++) cout << b[i] << " ";
cout << endl;
sort(b, 5);
cout << "In sorted order the doubles are:\n";
for (i = 0; i < 5; i++) cout << b[i] << " ";
cout << endl;
char c[7] = {'G', 'E', 'N', 'E', 'R', 'I', 'C'};
cout << "Unsorted characters:\n";
for (i = 0; i < 7; i++) cout << c[i] << " ";
cout << endl;
sort(c, 7);
cout << "In sorted order the characters are:\n";
for (i = 0; i < 7; i++) cout << c[i] << " ";
cout << endl;
16-9
return 0;
// This is the file sort.cpp. Algorithm abstraction example
template<class T>
void sort(T a[], int numberUsed) { sort.cpp
int indexOfNextSmallest;
for ( int index = 0; index < numberUsed - 1; index++){
//Place the correct value in a[index]:
indexOfNextSmallest = indexOfSmallest(a, index, numberUsed);
swapValues(a[index], a[indexOfNextSmallest]);
//a[0] <= a[1] <=...<= a[index] are the smallest of the original
//array elements. The rest of the elements are in the remaining
//positions.
}
}
template<class T>
void swapValues(T& var1, T& var2){
T temp;
temp = var1;
var1 = var2;
var2 = temp;
}
template<class T>
int indexOfSmallest( const T a[ ], int startIndex, int numberUsed) {
T min = a[startIndex];
int indexOfMin = startIndex;
for ( int index = startIndex + 1; index < numberUsed; index++)
if (a[index] < min){
min = a[index];
indexOfMin = index; //min is the smallest of a[startIndex] through a[index] .
}
return indexOfMin;
} 16-10
Class Templates
• Can also "generalize" classes template<class T>
– Can also apply to class definition
– All instances of "T" in class definition replaced by type parameter
– Just like for function templates!