Template - CSS342 Template Professor Munehiro Fukuda CSS342...

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

View Full Document Right Arrow Icon
CSS342: Template 1 CSS342: Template Professor: Munehiro Fukuda
Background image of page 1

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

View Full Document Right Arrow Icon
CSS342: Template 2 Today’s Topics Function templates Explained using a bubble sort example Class templates Explained using an insertion sort example Template compilation STL
Background image of page 2
CSS342: Template 3 Function Templates 1. If you focus on just integers Function Templates int myMin( int a, int b ) { return ( a < b ) ? a : b; } int main( ) { cout << myMin( 7, 5 ) << endl; } typedef double Object; Object myMin( Object a, Object b ) { return ( a < b ) ? a : b; } int main( ) { cout << myMin( 7.7, 5.5 ) << endl; } template <class Object> Object myMin( Object a, Object b ) { return ( a < b ) ? a : b; } int main( ) { cout << myMin( 7, 5 ) << endl; cout << myMin( 7.7, 5.5 ) << endl; } 2. If you use doubles instead 3. If you want to use both integers and doubles
Background image of page 3

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

View Full Document Right Arrow Icon
CSS342: Template 4 Function Template Example 1 Swap Function Templates #include <iostream> #include <string> #include <vector> using namespace std; template <class Object> { Object tmp = lhs; lhs = rhs; rhs = tmp; } int main( ) { int x = 5, y = 7; double a = 2, b = 4; string p = "abc", q = "xyz"; vector<int> v(5, 5), w(7, 7); mySwap( x, y ); cout << x << " " << y << endl; mySwap( a, b ); cout << a << " " << b << endl; mySwap( p, q ); cout << p << " " << q << endl; mySwap( v, w ); for ( int i = 0; i < v.size( ); i++ ) cout << v[i]; for ( int i = 0; i < w.size( ); i++ ) cout << w[i]; cout << endl; }
Background image of page 4
CSS342: Template 5 Function Template Example 2 Bubble Sort Function Templates 29 10 14 13 37 29 10 14 13 37 29 10 14 13 37 29 10 14 13 37 29 10 14 13 37 37 29 10 14 13 37 37 29 10 14 13 37 37 29 10 14 13 37 37 29 29 10 14 13 37 37 Pass 1 Pass 2 13 10 14 29 29 37 37 13 10 14 29 29 37 37 29 29 10 13 14 14 37 37 Pass 3 14 14 10 13 29 29 37 37 29 29 10 13 13 14 14 37 37 Pass 4
Background image of page 5

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

View Full Document Right Arrow Icon
CSS342: Template 6 Function Template Example 2 Bubble Sort (Continued) Function Templates #include <iostream>
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 16

Template - CSS342 Template Professor Munehiro Fukuda CSS342...

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

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