Templates and the Standard Template Library

Reviews
Shared by: alllona
Stats
views:
11
rating:
not rated
reviews:
0
posted:
11/15/2008
language:
UNKNOWN
pages:
0
Templates and the Standard Template Library Part I What are templates?   For ADTs such as stacks and queues, it is the manner in which the data is stored (rather than the precise type of data stored) that defines the ADT and its interface. The mechanism supporting genericity in C++ is the template construct.  This mechanism is important and powerful and is used throughout the Standard Template Library to achieve genericity. one template definition can be used to create multiple instances of a function (or class), each operating on (storing) a different type of data.  Templates provide a means to reuse code –  Function Genericity   Let’s way we want to swap the value of two integers ‘x’ and ‘y’ Few possible approaches:   . . . int temp; temp = x; x = y; y = temp; . . . int temp; temp = x; x = y; y = temp; . . . Write inline code Make the inline code into a function . . swap(x,y); . . . swap(x,y); . . . where, void swap(int&, int&) { int temp; temp = x; x = y; y = temp; } Function Genericity Now that we have a function to swap two ints, what if we want to write a function to swap two doubles.  Easiest approach might be to modify the earlier function by changing all int to double  Function overloading allows us to use the same name.  Function Genericity  Similarly, we can write any overloaded function to swap two strings or any other ADTs  A point to note is that the destructor, copy constructor and the assignment operator ‘=‘ should be defined/overloaded for the user defined ADTs specially if they member variables that are pointers. // to swap doubles void swap(double& x, double& y) { double temp; temp = x; x = y; y = temp; } // to swap strings void swap(string& x, string& y) { string temp; temp = x; x = y; y = temp; } // to swap ints void swap(int& x, int& y) { int temp; temp = x; x = y; y = temp; } Function Genericity  The next stage then is to replace the data type with a placeholder. This placeholder is termed a type parameter and the technique/mechanism used is called a template function. //template version of swap function template void swap(T& x, T& y) { T temp; temp = x; x = y; y = temp; } Template header Type parameter Function Genericity  More about function templates:   A function template is only a pattern that describes how individual functions can be constructed from given actual types. This process of constructing a function is called instantiation. In each instantiation, the type parameter is said to be bound to the actual type passed to it. The template header gives two pieces of information to the compiler  The following code is a function template from which a function can be created (so just store it for now and don’t convert it to m/c instructions)  The identifier (here, T) is the name of the type parameter for this function template that will be given a value when the function is called.  A function template can have more than one type parameter. Class Genericity Container classes like Stack, Queue, etc. might be needed to store any kind of data type and lend themselves to template implementations quite naturally.  The next slide shows a few approaches to enable creation of stack of different data types.  Class Genericity  Using ordinary classes // Stack of chars class Stack { public: Stack( ); void push (char); char pop( ) ; char peek( ); bool IsEmpty( ); bool IsFull( ); private: char data[10]; int top; }; // Stack of Fractions class Stack { public: Stack( ); void push (Fraction); Fraction pop( ) ; Fraction peek( ); bool IsEmpty( ); bool IsFull( ); private: Fraction data[10]; int top; }; // Stack of ints class Stack { public: Stack( ); void push (int); int pop( ) ; int peek( ); bool IsEmpty( ); bool IsFull( ); private: int data[10]; int top; }; Class Genericity  Using a typedef statement in class definition  reduces amount of code to modify // Stack of chars class Stack { public: typedef char item; Stack( ); void push (item); item pop( ) ; item peek( ); bool IsEmpty( ); bool IsFull( ); private: item data[10]; int top; }; // Stack of Fractions class Stack { public: typedef Fraction item; Stack( ); void push (item); item pop( ) ; item peek( ); bool IsEmpty( ); bool IsFull( ); private: item data[10]; int top; }; // Stack of ints class Stack { public: typedef int item; Stack( ); void push (item); item pop( ) ; item peek( ); bool IsEmpty( ); bool IsFull( ); private: item data[10]; int top; }; Class Genericity    Thus, we see even though using a typedef statement greatly reduces the code to be modified we still have to write three classes to be able to have a stack of ints, chars and Fractions. Moreover, if we have to use them in the same program, either we’ve to change their name or put them in different namespaces. A better solution is to use templates. Class Genericity  Using a template class // Stack Template Template class Stack { public: Stack( ); void push (item); item pop( ) ; item peek( ); bool IsEmpty( ); bool IsFull( ); private: item data[10]; int top; }; Class Genericity  More about class templates:   A class template is only a pattern that describes how a class can be constructed at the time of instantiation The template header gives two pieces of information to the compiler  The following code is a class template from which a class can be created (thus, just store it for now and don’t convert it to m/c instructions)  The identifier (here, T) is the name of the type parameter for this class template and will be given value at time of instantiation Class Genericity  Three important rules that govern building class templates:    All operations defined outside of the class declaration must be template functions Any use of the name of the template class as a type must be parameterized Operation on a template class should be defined in the same file as the class declaration  Might want to add another slide with the book code for the stack class before or after this slide //A Template Stack Class (from Section 7.2 in textbook) template class Stack { public: enum {DefaultStack =50,EmptyStack =-1 }; Stack(); Stack(int ); ˜Stack(); void push(const T&); void pop(); T topNoPop()const; bool empty()const; bool full()const; private: T*elements; int top; int size; void allocate(){elements =new T [size ]; top =EmptyStack;} void msg(const char*m )const {cout <<"***"<&); Contd. template Stack::Stack(){size =DefaultStack; allocate(); } template Stack::Stack(int s ){if (s <0) s*=-1; else if (s==0 ) s =DefaultStack; size =s; allocate(); } template Stack:: ˜Stack(){delete []elements;} template void Stack:: push(const T&e ){assert(!full()); if (!full())elements [++top ]=e; else msg("Stack full!");} template void Stack::pop(){assert(!empty()); if (!empty()) top--; Contd. template T Stack::topNoPop()const {assert(top >EmptyStack ); if (!empty())return elements [top ]; else {msg("Stack empty!"); T dummy_value; return dummy_value;}} template bool Stack::empty()const {return top <=EmptyStack;} template bool Stack::full()const {return top +1>=size;} template ostream&operator<<(ostream&os,const Stack&s ){ s.msg("Stack contents:"); int =s.top; while (t>s.EmptyStack )cout < intSt; Stack stringSt; // Stack template with function style parameter template class Stack { …same definition… }; Summary Templates make it possible for classes and functions to receive not only data values to be stored or operated on via parameter but also to receive the type of data via a parameter.  Templates go a long way in creating generic code and promoting code reuse. 

Related docs
Templates and the Standard Template Library
Views: 8  |  Downloads: 0
Standard templates
Views: 0  |  Downloads: 0
The Standard Template Library
Views: 14  |  Downloads: 0
Standard Template Library
Views: 19  |  Downloads: 1
The Standard Template Library (STL)
Views: 5  |  Downloads: 2
Standard Templates
Views: 103  |  Downloads: 3
Standard Template
Views: 0  |  Downloads: 0
THE C++STANDARD TEMPLATE LIBRARY (STL)
Views: 0  |  Downloads: 0
premium docs
Other docs by alllona
Assessment in Medical Education
Views: 419  |  Downloads: 15
Shout to the North
Views: 157  |  Downloads: 1
Massage Therapy for Subacute Low-Back Pain
Views: 727  |  Downloads: 26
at150
Views: 103  |  Downloads: 0
Western Union v Hill
Views: 561  |  Downloads: 2
Aronson Evidence Outline
Views: 514  |  Downloads: 19
dv101k
Views: 186  |  Downloads: 0
dv108k
Views: 129  |  Downloads: 0
cd200
Views: 106  |  Downloads: 0
at165
Views: 157  |  Downloads: 0
French MenuFoods Glossary
Views: 3193  |  Downloads: 77
de265
Views: 178  |  Downloads: 0
This is how we overcome
Views: 413  |  Downloads: 5
Hannan v Dusch
Views: 455  |  Downloads: 9
IPS Skeleton Outline
Views: 380  |  Downloads: 5