Lecture06-Template__STL - Computer Programming II 1...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Computer Programming II 1 Template & Standard Template Library Lecture 6 Computer Programming II 2 Learning Objectives Ø To understand how to define and when to use Function Template Ø To understand how to define and when to use Class Template Ø To understand the components of Standard Template Library (STL) Ø To understand how to define and when to use STL’s vector class, set class, and map class Computer Programming II 3 Template allows us to write generic functions or classes (called function template , class template ) that accept any data type as parameters or attributes. During compilation, the compiler will produce a separate definition for every data type that uses the template. Introduction Computer Programming II 4 Function Template void Swap ( int & m, int & n) { int temp = m; m = n; n = temp; } Recall the function to swap integers : To sort string , we would need a different function: void Swap ( string & s1, string & s2) { string temp = s1; s1 = s2; s2 = temp; } These 2 functions do the same thing, only different in the data type of objects they swap. We can avoid writing the redundant code by replacing both functions with a function template. Computer Programming II 5 template <typename T> void Swap( T & x, T & y) { T temp = x; x = y; y = temp; } int main() { int m = 22, n = 66; Swap(m, n); // integers cout << "m = " << m << endl << "n = " << n << endl; string s1 = "Michael", s2 = "Kelly"; Swap(s1, s2); // strings cout << "s1 = " << s1 << endl << "s2 = " << s2 << endl; } Output: m = 66 n = 22 s1 = Kelly s2 = Michael The symbol T is called type parameter . It is replaced by an actual type when the function is invoked. Function Template Template version of Swap() function: Compiler produces 2 definitions of Swap() functions: one for int , one for string Computer Programming II 6 template <typename T > // new, less confusing or template <class T > // old, confusing, class? Function templates are declared in the same way as ordinary functions, except that it is preceded by the specification below: and the type parameter T may be used in place of ordinary data types within the function definition. The word typename/class here means any type . A template may have several type parameters : Declaring Function Template template < typename T , typename U , typename V> Computer Programming II 7 Class templates are declared the same way as ordinary class, except that it is preceded by the specification below: template <typename T > class X { . .. }; A class template may have several type parameters . template < typename T , typename U> class X { . .. }; Declaring Class Template Computer Programming II 8 template <typename T> class Pair { T a; T b; public: void set ( T a, T b) { this->a = a; this->b = b; } T getA() { return a; } T getB() { return b; } }; Output: 3 5 Peter Jackson P J Define a class template Pair that have 2 attributes of the same type....
View Full Document

This note was uploaded on 02/03/2012 for the course IT 1241 taught by Professor Pang during the Spring '11 term at Multimedia University, Cyberjaya.

Page1 / 24

Lecture06-Template__STL - Computer Programming II 1...

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