150 likes | 456 Views
Templates in C++. Moshe Fresko Bar-Ilan University Object Oriented Programming 2007-2008. Templates. Templates A simple way to represent a wide range of general concepts. Direct support for generic programming.
E N D
Templates in C++ Moshe Fresko Bar-Ilan University Object Oriented Programming 2007-2008
Templates • Templates • A simple way to represent a wide range of general concepts. • Direct support for generic programming. • C++ Templates allow a type to be a parameter in the definition of a class or function • Standard Template Library • Every STL abstraction is represented as a template (string, ostream, list, map) • Also key operations like sort(v), output operator <<
Example: Simple String Template • String • A class that holds characters • To Provide operations like concatenation, substring, etc. • Different kinds of Strings • With signed char, or unsigned char • With Hebrew chars etc. • Expectation • The char type can be copied
Example: Simple String Template template<class C> class String { struct SRep ; SRep* rep ; public: String() ; String( const C* ) ; String( const String& ) ; int length() const ; char get( int index ) const ; … } ; // Usage in the program String<char> cs ; String<unsigned char> ucs ; String<wchar_t> wcs ; String<HebrewChar> hebstr ;
Example: Simple String Template • After suitable definitions String can be used as in: int main() { String<char> buf ; map<String<char>,int> m ; while (cin>>buf) m[buf]++ ; } int main() { String <HebrewChar> buf ; map<String<HebrewChar>, int> m ; while (cin>>buf) m[buf]++ ; }
string class in STL • STL provides basic_string as a template string type. template<class _E, class _Tr = char_traits<_E>, class _A = allocator<_E> > class basic_string { // ... } • It defines string with the regular char. typedef basic_string<char> string ; • Example usage: int main() { string buf ; map<string,int> m ; while (cin>>buf) m[buf]++ ; }
Defining a Template • Template member definitions are done in the same parameterized format template<class C> struct String<C>::SRep { C* s ; // Pointer to elements int sz ; // Number of elements int n ; // Reference count // … } ; template<class C> C String<C>::get( int index ) const { return rep->s[i] ; } template<class C> String<C>::String() { rep = new SRep(0,C()) ; }
Template Parameters • A template can take • Type parameters • Ordinary parameters such as int • Template parameters • Example template<class T, T default_value> class Cont { /* … */ } ; // Cont<int,5> a ; // Cont<Complex,Complex(3,5)> b ; template<class T, int max_size> class Buffer { T v[max_size] ; int sz ; public: Buffer() : sz(max_size) { } // … } ; // Buffer<char,127> cbuf ; // Buffer<Record,8> rbuf ;
Function Templates • Declaration template<class T> void sort(vector<T>&) ; • Usage void f(vector<int>& vi, vector<string>& vs) { sort(vi) ; // sort<int>(vi) ; sort(vs) ; // sort<string>(vs) ; } • Definiton template<class T> void sort(vector<T>& v) { for (int gap=v.size()/2;0<gap;gap/=2) for (int i=gap;i<n;++i) for (int j=i-gap;0<=j;j-=gap) if (v[j+gap]<v[j]) swap(v[j],v[j+gap]) ; } • operator < is used for comparison, so for any T it must have been defined.
Template Arguments to Specify Strategy template<class T, class C> int compare ( const String<T>& str1, const String<T>& str2 ) { for ( int i=0;i<str1.length() && i<str2.length();++i) if(!C::eq(str1[i],str2[i])) return C::lt(str1[i],str2[i]) ? -1 : +1 ; return str1.length()-str2.length() ; } template<class T> class Cmp { // Default public: static int eq(T a,T b) { return a==b ; } static int lt(T a, T b) { return a<b ; } } ; // We can define new comparisons like: // different alphabets or case-insensitive etc. // Default definition can be provided: // template<class T, class C=Cmp<T> >
Exercises • Write Stack class with a single template class. • Complete the String class. • Define a sort function that takes a comparison criteria. Define a class Point including real valued x and y coordinates. Run the sort algorithm for sorting according to x first and then according to y first. • Write a template sum function that gets as an argument a list of elements.