Docsity

Templates - Data Structures - Notes | CS 310, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
CS 310 A Few C++ Features, 1
'
&
$
%
Templates
We have got a BST class that can hold integers and that allows
users to specify whatever processing they want.
But, how about floating point numbers, names, or personal records?
•Solution 1: copy and modify.
•Solution 2: let the machine do the tedious job; use templates.
CS 310 A Few C++ Features, 2
'
&
$
%
Example 1: A Template Swap Function
// No machine instructions generated here. The compiler
// merely memorizes the template of swap functions.
template <class T>
void swap (T& x, T& y) {
T tmp=x; x = y; y = tmp;
}
main () {
int a, b; float c, d;
swap (a, b); // copy and paste; function
// void swap (int, int)
// created ‘‘on spot’’ before invoked
swap (c, d); // copy and paste; function
// void swap (float, float)
// created ‘‘on spot’’ before invoked
}
pf3
pf4
pf5

Partial preview of the text

Download Templates - Data Structures - Notes | CS 310 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Templates

We have got a BST class that can hold integers and that allows users to specify whatever processing they want. But, how about floating point numbers, names, or personal records?

  • Solution 1: copy and modify.
  • Solution 2: let the machine do the tedious job; use templates.

CS 310 A Few C++ Features, 2 ' $

Example 1: A Template Swap Function

// No machine instructions generated here. The compiler // merely memorizes the template of swap functions. template void swap (T& x, T& y) { T tmp=x; x = y; y = tmp; }

main () { int a, b; float c, d; swap (a, b); // copy and paste; function // void swap (int, int) // created ‘‘on spot’’ before invoked swap (c, d); // copy and paste; function // void swap (float, float) // created ‘‘on spot’’ before invoked }

Pointer to Functions

  • A function name in a C++ program is actually an address – the starting address of the machine instructions of the functions.
  • You can store such an address in a pointer, called function pointers.
  • Example double sin(double x) { ... computate sin(x) ... } double cos(double x) { ... computate cos(x) ... }

CS 310 A Few C++ Features, 4 ' $

main() { double a; double (p) (double); // declare a pointer to functions // that accept a double parameter // and return a double value p = sin; // have p points to function sin() p(a); // compute sin(a) // you can also use noatation (p)(a); p = cos; // have p points to function cos() p(a); // compute cos(a) }

Function Parameters

A function parameter is a pointer parameter that points to functions.

Motive: In the tree traversal functions, the “processing” (that is, printing) applied to each node is fixed. How can we, the designer of the BST, anticipate all the applications/processings to be used with tree traversals?

Solution: We cannot. Instead, let the user of the traversal functions to determine the processing of each node and pass us a pointer to the desired function.

CS 310 A Few C++ Features, 8 ' $

Inorder Traversal with A Function Parameter

void inorder (TreeNode* p, void (f )(int&)) { if (p != NULL) { inorder (p->left, f); f (p->data); inorder (p->right, f); } } Alternatively, you can call the function by (f)(p->data);

Two Possible Node-Processing Functions

  • void print_int (int& d) { cout << d << ’\n’; } To print the whole tree, invoke inorder (root, print int);
  • void increment (int& d) { d++; } To increase the value of every node by 1, invoke inorder (root, increment);