Jt-Polys-Cours-11.Pdf

Jt-Polys-Cours-11.Pdf

Notes de cours Standard Template Library ' $ ' $ Ecole Nationale d’Ing´enieurs de Brest Table des mati`eres L’approche STL ................................... 3 Les it´erateurs .................................... 15 Les classes de fonctions .......................... 42 Programmation par objets Les conteneurs ................................... 66 Le langage C++ Les adaptateurs ................................. 134 — Standard Template Library — Les algorithmes g´en´eriques ...................... 145 Index ........................................... 316 J. Tisseau R´ef´erences ...................................... 342 – 1996/1997 – enib c jt ........ 1/344 enib c jt ........ 2/344 & % & % Standard Template Library L’approche STL ' $ ' $ L’approche STL Biblioth`eque de composants C++ g´en´eriques L’approche STL Fonctions : algorithmes g´en´eriques 1. Une biblioth`eque de composants C++ g´en´eriques sort, binary search, reverse, for each, accumulate,... 2. Du particulier au g´en´erique Conteneurs : collections homog`enes d’objets 3. Des indices aux it´erateurs, via les pointeurs vector, list, set, map, multimap,... 4. De la g´en´ericit´edes donn´ees `ala g´en´ericit´edes structures It´erateurs : sorte de pointeurs pour inspecter un conteneur input iterator, random access iterator, ostream iterator,... Objets fonction : encapsulation de fonctions dans des objets plus, times, less equal, logical or,... Adaptateurs : modificateurs d’interfaces de composants stack, queue, priority queue,... enib c jt ........ 3/344 enib c jt ........ 4/344 & % & % L’approche STL L’approche STL ' $ ' $ Du particulier . au g´en´erique int template <class T> max(int x, int y) { return x < y? y : x; } const T& max(const T& x, const T& y) { return x < y? y : x; } int template <class T, class Compare> max(int x, int y, int (*compare)(int,int)) { const T& return compare(x,y)? y : x; max(const T& x, const T& y, Compare compare) { } return compare(x, y)? y : x; } enib c jt ........ 5/344 enib c jt ........ 6/344 & % & % L’approche STL L’approche STL ' $ ' $ Des indices . ... via les pointeurs ... int int* max element(const int* array, int n) { max element(int* first, int* last) { int result = array[0]; if(first == last) return first; for(int i = 1; i < n; i++) int* result = first; if(result < array[i]) result = array[i]; while(++first!= last) return result; if(*result < *first) result = first; } return result; } enib c jt ........ 7/344 enib c jt ........ 8/344 & % & % L’approche STL L’approche STL ' $ ' $ . aux it´erateurs g´en´eriques max element (2) STL algo.h template <class InputIterator> template <class InputIterator, class Compare> InputIterator InputIterator max element(InputIterator first, InputIterator last) { max element(InputIterator first, InputIterator last, if(first == last) return first; Compare compare) { InputIterator result = first; if (first == last) return first; while(++first!= last) InputIterator result = first; if(*result < *first) result = first; while(++first!= last) return result; if(compare(*result,*first)) result = first; } return result; } enib c jt ........ 9/344 enib c jt ....... 10/344 & % & % L’approche STL L’approche STL ' $ ' $ Exemple d’utilisation : max element (2) De la g´en´ericit´edes donn´ees . bool str compare(const char* a, const char* b) { // Sequences return : :strcmp(a, b) < 0? 1 : 0; template <class T> class vector; } template <class T> class deque; template <class T> class list; char* names[] = { "Dany", "Alexis", "Serge", "Vincent" } ; // Associative containers cout << *max element(names, names + 4, str compare) << endl; template <class T1, class T2> class pair; template <class Key, class Compare> class set; template <class Key, class Compare> class multiset; template <class Key, class T, class Compare> class map; template <class Key, class T, class Compare> class multimap ; enib c jt ....... 11/344 enib c jt ....... 12/344 & % & % L’approche STL L’approche STL ' $ ' $ . `ala g´en´ericit´edes structures Exemple d’utilisation : stack // Containers adaptors stack< deque<int> > s1; template <class Container> class stack; stack< vector<int> > s2; template <class Container> class queue; template <class Container> class priority queue ; s1.push(42); s1.push(101); s1.push(69); copy(s1.begin(), s1.end(), s2.begin()); // Instantiations stack< vector<double> > s; while( !s2.empty()) { queue< deque<const char*> > q; cout << s2.top() << endl; priority queue< list<int> > p; s2.pop() ; } enib c jt ....... 13/344 enib c jt ....... 14/344 & % & % Standard Template Library It´erateurs ' $ ' $ Les it´erateurs Des pointeurs sp´ecialis´es (p2 < p) -= m p += n (p1 > p) • •Z • Z Z Les it´erateurs -- ++Z ? ? = ? Z~ ? ? 1. Des pointeurs sp´ecialis´es ........................ ....... 16 *p 2. output iterator ........................................ 18 p[n] Pointer 3. input iterator<T,Distance> ............................. 21 t1 = *p; *p = t2; 4. forward iterator<T,Distance> ........................... 25 p bidirectional iterator<T,Distance> •Z 5. ..................... 29 Z Z ++Z 6. random access iterator<T,Distance> ..................... 34 ? Z~ 7. It´erateurs d´eriv´es ............................. ......... 36 *p InputIterator 8. Exemples d’utilisation ............................ ...... 38 t1 = *p; enib c jt ....... 15/344 enib c jt ....... 16/344 & % & % It´erateurs It´erateurs ' $ ' $ It´erateurs de base output iterator p •Z Z Z ++Z struct output iterator {} ; ? Z~ *p OutputIterator template <class T, class Distance> struct input iterator {} ; *p = t2; template <class T, class Distance> class OutputIterator : public output iterator { struct forward iterator {} ; public : T& operator*(void); template <class T, class Distance> OutputIterator& operator++(void); struct bidirectional iterator {} ; OutputIterator operator++(int); } ; template <class T, class Distance> struct random access iterator {} ; enib c jt ....... 17/344 enib c jt ....... 18/344 & % & % It´erateurs It´erateurs ' $ ' $ Exemple de d´efinition : output iterator (1) Exemple de d´efinition : output iterator (2) template <class T> OstreamIterator<T>& operator=(const T& value) { class OstreamIterator : public output iterator { * stream << value; protected : if( string) * stream << string ; ostream* stream ; return *this; char* string ; } public : OstreamIterator<T>& operator*(void) { return *this; } OstreamIterator(ostream& s) : stream(&s), string(0) {} OstreamIterator<T>& operator++(void) { return *this; } OstreamIterator(ostream& s, char* c) OstreamIterator<T>& operator++(int) { return *this; } : stream(&s), string(c) {} } ; // end class OstreamIterator<T> `asuivre ... ... fin enib c jt ....... 19/344 enib c jt ....... 20/344 & % & % It´erateurs It´erateurs ' $ ' $ input iterator<T,Distance> Exemple de d´efinition : input iterator<T,Distance> (1) p •Z Z Z ++Z template <class T> ? Z~ class IstreamIterator : public input iterator<T,ptrdiff t> { *p InputIterator protected : t1 = *p; istream* stream ; T value; bool endMarker ; template <class T, class Distance> class InputIterator : public input iterator<T,Distance> { void read() { public : endMarker = (* stream)? true : false; const T& operator*(void) const; if( endMarker) * stream >> value; InputIterator<T,Distance>& operator++(void) ; endMarker = (* stream)? true : false; InputIterator<T,Distance> operator++(int) ; } } ; `asuivre ... enib c jt ....... 21/344 enib c jt ....... 22/344 & % & % It´erateurs It´erateurs ' $ ' $ Exemple de d´efinition : input iterator<T,Distance> (2) Exemple de d´efinition : input iterator<T,Distance> (3) public : friend bool operator==(const IstreamIterator<T>& x, IstreamIterator(void) const IstreamIterator<T>& y) { : stream(&cin), endMarker(false) {} return x. stream == y. stream && IstreamIterator(istream& s) : stream(&s) { read() ; } x. endMarker == y. endMarker const T& operator*(void) const { return value ; } || IstreamIterator<T>& operator++(void) { x. endMarker == false && read(); return *this; y. endMarker == false; } } IstreamIterator<T> operator++(int) { } ; // end class IstreamIterator<T> IstreamIterator<T> tmp = *this; read(); return tmp; ... fin } `asuivre ... enib c jt ....... 23/344 enib c jt ....... 24/344 & % & % It´erateurs It´erateurs ' $ ' $ forward iterator<T,Distance> Exemple de d´efinition : forward iterator (1) p •Z Z Z ++Z template <class T> ? Z~ struct list node { void* next; T data; } ; *p ForwardIterator t1 = *p; *p = t2; template <class T> class list iterator : public forward iterator<T,ptrdiff t> { protected : template <class T, class Distance> class ForwardIterator : public forward iterator<T,Distance> { list node<T>* node; public : list iterator(list node<T>* x) : node(x) {} T& operator*(void); const T& operator*(void) const; `asuivre ... ForwardIterator& operator++(void) ; ForwardIterator operator++(int); } ; enib c jt ....... 25/344 enib c jt ....... 26/344 & % & % It´erateurs It´erateurs ' $ ' $ Exemple de d´efinition : forward iterator (2) Exemple de d´efinition : forward iterator (3) public : list iterator& operator++(void) { list iterator(void) {} node = (list node*)((* node).next); return *this; } bool operator==(const list iterator& x) const { return node == x. node; list iterator operator++(int) { } list iterator tmp = *this; ++*this; return tmp; } T& operator*(void) const { return (* node).data ; } } ; ... fin `asuivre ... enib c jt ....... 27/344 enib c jt ....... 28/344 & % & % It´erateurs It´erateurs ' $ ' $ bidirectional iterator<T,Distance> Exemple de d´efinition : bidirectional iterator (1) p •Z Z Z -- ++Z = ? Z~ template <class T> struct

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    86 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us