1 / 16

CSS342: Template

CSS342: Template. Professor: Munehiro Fukuda. Today’s Topics. Function templates Explained using a bubble sort example Class templates Explained using an insertion sort example Template compilation STL. Function Templates. Function Templates. If you use doubles instead.

anise
Télécharger la présentation

CSS342: Template

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSS342: Template Professor: Munehiro Fukuda 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 CSS342: Template

  3. Function Templates Function Templates • If you use doubles instead typedef double Object; Object myMin( Object a, Object b ) { return ( a < b ) ? a : b; } int main( ) { cout << myMin( 7.7, 5.5 ) << endl; } int myMin( int a, int b ) { return ( a < b ) ? a : b; } int main( ) { cout << myMin( 7, 5 ) << endl; } • If you focus on just integers • If you want to use both integers and doubles 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; } CSS342: Template

  4. Function Templates Function Template Example 1Swap 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; } #include <iostream> #include <string> #include <vector> using namespace std; template <class Object> void mySwap( Object &lhs, Object &rhs ) { Object tmp = lhs; lhs = rhs; rhs = tmp; } CSS342: Template

  5. Function Templates Function Template Example 2Bubble Sort Pass 1 Pass 2 Pass 3 29 10 14 37 13 10 14 29 13 37 10 14 13 29 37 10 29 14 37 13 10 14 29 13 37 10 14 13 29 37 10 14 29 37 13 10 14 29 13 37 10 13 14 29 37 10 14 29 37 13 10 14 13 29 37 Pass 4 10 14 29 13 37 10 13 14 29 37 10 13 14 29 37 CSS342: Template

  6. Function Templates i i+1 15 10 swap Function Template Example 2Bubble Sort (Continued) #include <iostream> #include <vector> #include <string> using namespace std; template <class Object> void bubbleSort( vector<Object> & a ) { bool swapOccurred = true; // true when swaps occur for ( int pass = 1; ( pass < a.size( ) ) && swapOccurred; ++pass ) { swapOccurred = false; // swaps have not occurred at the beginning for ( int i = 0; i < a.size( ) - pass; ++i ) { // a bubble(i) goes from 0 to size - pass if ( a[i] > a[i + 1] ) { swap( a[i], a[i + 1] ); swapOccurred = true; // a swap has occured } } } } CSS342: Template

  7. Function Templates Function Template Example 2Bubble Sort (Continued) int main( ) { vector<int> intArray; // The integer array int x; // An integer to read cout << "Enter positive integers to sort: “ << endl; while ( true ) { cin >> x; if ( x > 0 ) intArray.push_back( x ); else break; } bubbleSort( intArray ); vector<string> strArray; // The string array string s; // a string to read cout << "Enter strings to sort: “ << endl; while( cin >> s ) strArray.push_back( s ); bubbleSort( strArray ); cout << "Sorted strings are: " << endl; for ( int i = 0; i < strArray.size( ); i++ ) cout << strArray[i] << " "; cout << endl; return 0; } CSS342: Template

  8. Class Templates Class Templates • Private data members and a method’s return value may be any class. • Consider a sorted list whose item may be a string or an integer: class SortedList { public: void insert( const string_or_int &item ); const SortedList &operator+( const SortedList &rhs ); void print( ) const; private: string_or_int array; void insertionSort( ); }; int main( ) { SortedList<string> strList; strList.insert( “abc” ); SortedList<int> intList; intList.insert( 5 ); } Sorted c d b a f a b c d e insert( ) Sorted 3 4 2 1 5 1 2 3 4 5 insert( ) CSS342: Template

  9. Class Templates How to Define the Template Header in .h (Sorted List Example) #ifndef _SORTEDLIST_H_ #define _SORTEDLIST_H_ #include <iostream> #include <vector> using namespace std; template<class Object> class SortedList { public: void insert( const Object &item ); const SortedList<Object> &operator+( const SortedList<Object> &rhs ); void print( ) const; private: vector<Object> array; void insertionSort( ); }; #include "SortedList.cpp“ // discuss about this technique #endif At compilation time, Object will be replaced with int or string CSS342: Template

  10. Class Templates How to Code the Template Implementation in .cpp (Sorted List Example) #ifndef _SORTEDLIST_CPP_ #define _SORTEDLIST_CPP_ template <class Object> void SortedList<Object>::insert( const Object &item ) { array.push_back( item ); // insert a new item insertionSort( ); // sort items } template <class Object> const SortedList<Object> &SortedList<Object>:: operator+( const SortedList<Object> &rhs ) { // concatenate two lists SortedList<Object> *list = new SortedList<Object>; list->array = array; for ( int i = 0; i < rhs.array.size( ); i++ ) list->array.push_back( rhs.array[i] ); return *list; } At compilation time, Object will be replaced with int or string CSS342: Template

  11. Class Templates How to Code the Template Implementation in .cpp (Sorted List Example) template <class Object> void SortedList<Object>::print( ) const { // print out all sorted item for ( int i = 0; i < array.size( ); i++ ) cout << array[i] << " "; cout << endl; } template <class Object> void SortedList<Object>::insertionSort( ) { for ( int unsorted = 1; unsorted < array.size( ); ++unsorted ) { // Assume the 0th item is sorted. Unsorted items start from the 1st item Object unsortedTop = array[unsorted]; // Copy the top of unsorted group int i; for ( i = unsorted - 1; ( i >= 0 ) && (array[i] > unsortedTop ); --i ) // Upon a successful comparison, shift array[i] to the right array[i + 1] = array[i]; // insert the unsorted top into the sorted group; array[i + 1] = unsortedTop; } } #endif See the next slide for graphical explanation unsortedTop 13 insert copy compare unsorted 2 3 8 10 14 29 37 13 11 25 20 loc loc+1 loc loc+1 shift loc loc+1 14 29 37 CSS342: Template

  12. Class Templates Insertion Sort Sorted Unsorted 29 10 14 37 13 Copy 10 Shift 29 29 29 14 37 13 10 29 14 37 13 Insert 10, copy 14 unsortedTop 10 29 29 37 13 Shift 29 10 14 29 37 13 Insert 14; copy 37 10 14 29 37 13 Shift nothing 10 14 29 37 13 Insert 37; Copy 13 10 14 14 29 37 Shift 37, 29 and 14. 10 13 14 29 37 Insert 13 CSS342: Template

  13. Template Compilation Tips for Template Compilation • You cannot compile SortedList.cpp independently. • SortedList.cpp should be treated as if it were a part of a header file. • SortedList.h: at the end of this header file, add: • #include “SortedList.cpp” • SortedList.cpp: at the beginning of this cpp file, do Not add: • #include “SortedList.h” • SortedListDriver.cpp • Compile SortedListDriver.cpp only. • In Eclipse, it is a bit tricky to exclude SortedList.cpp from compilation. To make it simple, we could name it as “SortedList.cpp.h”. CSS342: Template

  14. Standard Template Library Iterators • A software design pattern that abstracts the process of scanning through a collection of elements • The index of vector [i] can be considered as a primitive iterator which is however not enough… • The iterator should include: • The current position (like vector[i]) • The retrieval of the current item (like vector[i]) • The way of stepping to the next position (forward or backward) CSS342: Template

  15. Standard Template Library Vector and Iterators: Vector has forward and reverse iterators: #include <iostream> #include <vector> using namespace std; int main( ) { vector<int> array; for ( int i = 0; i < 10; i++ ) array.push_back( i ); vector<int>::iterator f; // forward iterator for ( f = array.begin( ); f != array.end( ); f++ ) cout << *f << endl; vector<int>::reverse_iterator b; // reverse iterator for ( b = array.rbegin( ); b != array.rend( ); b++ ) cout << *b << endl; } 0 1 2 3 4 5 6 7 8 9 CSS342: Template

  16. Standard Template Library Standard Template Library • Standard Template Library: • Provides typical templates such as vector, list, stack, and queue. • At least, we have studied “vector” already. • Containers: • Elements stored in STL template class • Iterators: • A pointer to a container in operation. CSS342: Template

More Related