Presentation is loading. Please wait.

Presentation is loading. Please wait.

Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,

Similar presentations


Presentation on theme: "Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,"— Presentation transcript:

1 Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray, charArray, stringArray

2 template 格式 : template class classname { …. }; - e.g., class Array of any type

3 template class Array { public: T& operator[ ] (int); Array(int); ~Array( ); int get_size( ) const { return size; } private: T* a; int size; T dummy_val; };

4 template T& Array ::operator[ ](int i) { if (i = size) { cerr << “index “ << I << “out of bounds”; return dummy_val; } return a[i]; } template const T& Array ::operator[ ](int i) const { if (i = size) { cerr << “index “ << I << “out of bounds”; return dummy_val; } return a[i]; }

5 template Array ::Array(int s) { a = new T[size =s]; } …

6 template int main( ) { Array a(100); // an integer array Array b(200); // a char array Array c(300); // a float array a[10] = 30; b[15] = ‘s’; c[20] = 35.99; … }

7 template more than one class parameters - e.g. template class Sample { public: T2 m(T3 p) { … } private: T1 x; … };

8 template function-style parameters - e.g. template class Array { public: Array( ); T& operator[ ] (int); … private: T* a; int size; T dummy_val; }; template Array ::Array( ) { a = new T[size = s]; }

9 template void main( ) { Array a1; Array a2; a1[5] = 20.9; a2[30] = ‘c’; … }

10 template Example: A template Stack class - p. 353 int main( ) { Stack s1; Stack s2; … }

11 template function templates (top-level functions) - e.g. template T min(const T &a, const T &b) { if (a < b) return a; else return b; }

12 function template (cont.) int main ( ) { int i=10; j=20; cout << min(i, j) <<endl; // integer function float f1=30.8; f2=40.4; cout << min(f1, f2) << endl; // float function … }

13 function template (example) template void Insertionsort(T a[ ], int n) { for (int i=1; i<n; i++) { if (a[ i ] < a[i-1]) { T v=a[ i ]; int j = i; do { a[ j ] = a[j-1]; --j; } while ((j>0) && (v <a[j-1])); a[ j ] = v; }

14 function template (cont.) int A[100]; float B[1000]; … Insertionsort(A, 100); Insertionsort(B,1000); …

15 Standard Template Library(STL) STL - a part of the standard C++ library - provide C++ with data structures Three elements in STL - Containers - template classes - Algorithms - functions that process the contents of containers - Iterators - “Pointers” for accessing the container’s objects

16 standard template library example: #include using namespace std; int main( ) { vector v; // container deque d; // container … sort(v.begin( ), v.end( )); // algorithm and iterators sort(d.begin( ), d.end( )); … }

17 standard template library basic containers - sequential - list, vector, deque - associative - set, multiset, map, multimap

18 standard template library Member functions for all containers - Default constructor, copy constructor, destructor - empty - max_size, size - = >= == != - swap Functions for first-class containers - begin, end - rbegin, rend - erase, clear, insert

19 standard template library vector - an array that grows and shrinks as needed - e.g. vector d; // no need to specify size vector a(100); // initialize 100 elements to 0, still grow // dynamically - more examples……

20 STL -- vector #include using namespace std; int main( ) { int i; vector nums; nums.insert(nums.begin( ), -999); nums.insert(nums.begin( ), 14); nums.insert(nums.end( ), 57); for (i = 0; i < num.size( ); i++) cout << nums[ i ] << endl;

21 STL -- vector cout << endl; nums.erase(nums.begin( )); for (i = 0; i < nums.size( ), i++) cout << nums[ i ] << endl; return 0; } nums nums.begin( ) nums.end( ) 14 -999 57

22 standard template library iterators - a mechanism for accessing the objects in a container one at a time - similar to “pointer’ but no need to handle address - const_iterator is used if the iteration does not change any of container’s elements - i.e. iterator for read/write const_iterator for read only

23 STL -- list #include using namespace std; void dump( list & ); int main( ) { list names; names.insert(names.begin( ), “Kamiko”); names.insert(names.end( ), “Andre”); names.insert(names.begin( ), “Chengwen”); names.insert(names.begin( ), “Maria”); dump(names);

24 STL -- list (cont.) names.reverse( ); cout << endl; dump(names); return 0; } void dump( list & l) { list ::const_iterator it; it = l.begin( ); while (it != l.end( )) { cout << *it << endl; it++; } Maria Chengwen Kamiko Andre Kamiko Chengwen Maria

25 standard template library iterators - e.g. list names; vector dv; deque my_q; list ::iterator it; list ::const_iterator cit; vector ::iterator vit; deque ::const_iterator cdit;

26 iterators it = names.begin( ); cout << *it << endl; it++; vit = dv.begin( ); while (vit != dv.end( )) { … } … operations of iterators - ++p, p++, *p, p=p1, p==p1, p!=p1, --p, p--, p+i, p-i, p[i], p+=i, p-=i (p, p1: iterators, i: integer)

27 standard template library four types of iterators - iterator forward read/write - const_iterator forward read - reverse_iterator backward read/write - const_reverse_iterator backward read

28 standard template library efficiency of vectors, deques, and lists Operation vector deque list -------------------------------------------------------------- insert/erase at beginning linear constant constant insert/erase at end constant constant constant insert/erase in middle linear linear constant access first element constant constant constant access last element constant constant constant access middle element constant constant linear

29 standard template library vector - good for insertion(deletion) at the end - less efficient to insert at the beginning deque - equally efficient for insertion(deletion) at both ends - both vector and dequeue overload [ ] operator (not for list)

30 basic associative containers set - a collection of zero or more nonduplicate unordered elements called keys - e.g. { 10, 20, 300, 25 } set of integers multiset - allow duplicate keys

31 basic associative containers map - a collection of zero or more unordered pairs; in each pair, one element is a nonduplicate key and the other is a value associated with the key - e.g {(1, “Jan”), (2, “Feb”), (3, “Mar”)} multimap - allow duplicate keys

32 set -- example #include using namespace std; int main( ) { set s; s.insert(-999); s.insert(18) s.insert(321); s.insert(-999); // duplicate – not inserted set ::const_iterator it; it = s.begin( ); while ( it != s.end( )) cout << *it++ << endl;

33 set -- example (cont.) int key; cout << “Enter an integer:”; cin >> key; it = s.find(key); if ( it == s.end( )) cout << key << “is not in set.” << endl; else cout << key << “is in set.” << endl; return 0; } -999 18 321 Enter an integer: 29 29 is not in set.

34 map -- example #include using namespace std; int main( ) { map m; m[“zero”] = 0; m[“one”] = 1; m[“two”] = 2; m[“three”] = 3; m[“foru”] = 4; m[“five”] = 5; m[“six”] = 6; m[“seven”] = 7; m[“eight”] =8; m[“nine”]; 9;

35 map – example (cont.) cout << m[“three”] << endl // 3 << m[“five”] << endl // 5 << m[“seven”] << endl; // 7 return 0; }

36 standard template library container adaptor - adapts a container to behave in a particular way - e.g. by default, STL stack adapts a deque - three container adaptors: stack, queue, priority queue

37 container adaptor stack - a LIFO (Last In First Out) data structure - by default, adapt deque queue - a FIFO (First In First Out) data structure - by default, adapt deque priority queue - a queue whose elements are removed in some priority order - by default, adapt vector

38 priority queue -- example #include #include // for rand and srand #include // for time using namespace std; int main( ) { const int howMany=8; int i; priority_queue nums; srand(time(0)); // seed rand

39 priority queue – example (cont.) for (i = 0; i < howMany; i++) { int next = rand( ); cout << next << endl; nums.push(next); } cout << “\n*** Priority by value:” << endl; for ( i = 0; i < howMany; i++) { cout << nums.top( ) << endl; nums.pop( ); } return 0; } 9614 691 21890 3936 20054 369 3142 1191 *** Priority by value: 21890 20054 8614 3936 3142 1191 691 369

40 standard template library other containers - string, bitset algorithms - implemented as template functions, including - sorting - searching - numerical processing - set operations - copying - ref. p. 533

41 STL algorithms #include using namespace std; void printChar(char c) { cout << c; } int main ( ) { string s=“pele, the greatest ever”; cout << “s: “ << s << endl; cout << “s in reverse: “; for_each(s.rbegin( ), s.rend( ), printChar); cout << endl;

42 STL algorithms (cont.) char* where = find(s.begin( ), s.end( ), ‘a’); cout << “ ‘a’ is the “ << where – s.begin( ) + 1 << “th char in: << s << endl; random_shuffle(s.begin( ), s.end( )); cout << “s after a random shuffle: “ << s << endl; where = find(s.begin( ), s.end( ), ‘a’); cout << “ ‘a’ is the “ << where – s.begin( ) + 1 << “th char in: << s << endl; sort(s.being( ), s.end( )); cout << “ s sorted in ascending order: “ << s << endl; return 0; }

43 STL algorithms #include using namespace std; void dump (int i) { cout << i << endl;} bool odd(int i) { return i%2 != 0;} bool comp(const int& i1, const int& i2) { return i1 > i2;} int main( ) { vector v(10); generate(v.begin( ), v.end( ), rand); replace_if(v.begin( ), v.end( ), odd, 0);

44 STL algorithms (cont.) sort(v.begin( ), v.end( ), comp); for_each(v.begin( ), v.end( ), dump); return 0; }


Download ppt "Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,"

Similar presentations


Ads by Google