Notes de cours Standard 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, , map, ,... 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 , 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, ,...

enib c jt ...... 3/344 enib c jt ...... 4/344 & % & % L’approche STL L’approche STL ' $ ' $ Du particulier ...... au g´en´erique

int template 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 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 template 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 vector; } template class deque; template class list; char* names[] = { "Dany", "Alexis", "Serge", "Vincent" } ; // Associative containers cout << *max element(names, names + 4, str compare) << endl; template class pair; template class set; template class multiset; template class map; template 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 > s1; template class stack; stack< vector > s2; template class queue; template class priority queue ; s1.push(42); s1.push(101); s1.push(69); copy(s1.begin(), s1.end(), s2.begin()); // Instantiations stack< vector > s; while( !s2.empty()) { queue< deque > q; cout << s2.top() << endl; priority queue< list > 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 ...... 21 t1 = *p; *p = t2; 4. forward iterator ...... 25 p bidirectional iterator •Z 5...... 29 Z Z ++Z 6. random access iterator ...... 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 struct input iterator {} ; *p = t2;

template class OutputIterator : public output iterator { struct forward iterator {} ; public : T& operator*(void); template OutputIterator& operator++(void); struct bidirectional iterator {} ; OutputIterator operator++(int); } ; template 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 OstreamIterator& operator=(const T& value) { class OstreamIterator : public output iterator { * stream << value; protected : if( string) * stream << string ; ostream* stream ; return *this; char* string ; } public : OstreamIterator& operator*(void) { return *this; } OstreamIterator(ostream& s) : stream(&s), string(0) {} OstreamIterator& operator++(void) { return *this; } OstreamIterator(ostream& s, char* c) OstreamIterator& operator++(int) { return *this; } : stream(&s), string(c) {} } ; // end class OstreamIterator `asuivre ...... fin

enib c jt ...... 19/344 enib c jt ...... 20/344 & % & % It´erateurs It´erateurs ' $ ' $

input iterator Exemple de d´efinition : input iterator (1)

p •Z Z Z ++Z template ? Z~ class IstreamIterator : public input iterator { *p InputIterator protected : t1 = *p; istream* stream ; T value; bool endMarker ; template class InputIterator : public input iterator { void read() { public : endMarker = (* stream)? true : false; const T& operator*(void) const; if( endMarker) * stream >> value; InputIterator& operator++(void) ; endMarker = (* stream)? true : false; InputIterator operator++(int) ; } } ; `asuivre ...

enib c jt ...... 21/344 enib c jt ...... 22/344 & % & %

It´erateurs It´erateurs ' $ ' $

Exemple de d´efinition : input iterator (2) Exemple de d´efinition : input iterator (3)

public : friend bool operator==(const IstreamIterator& x, IstreamIterator(void) const IstreamIterator& 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& operator++(void) { x. endMarker == false && read(); return *this; y. endMarker == false; } } IstreamIterator operator++(int) { } ; // end class IstreamIterator IstreamIterator tmp = *this; read(); return tmp; ... fin } `asuivre ...

enib c jt ...... 23/344 enib c jt ...... 24/344 & % & % It´erateurs It´erateurs ' $ ' $

forward iterator Exemple de d´efinition : forward iterator (1)

p •Z Z Z ++Z template ? Z~ struct list node { void* next; T data; } ; *p ForwardIterator t1 = *p; *p = t2; template class list iterator : public forward iterator { protected : template class ForwardIterator : public forward iterator { list node* node; public : list iterator(list node* 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 Exemple de d´efinition : bidirectional iterator (1)

p •Z  Z  Z -- ++Z = ? Z~ template struct list node { void* next; void* previous; T data; } ; *p BidirectionalIterator t1 = *p; *p = t2; template class list iterator

template : public bidirectional iterator { class BidirectionalIterator : public bidirectional iterator { protected : public : list node* node; T& operator*(void); const T& operator*(void) const; list iterator(list node* x) : node(x) {} ForwardIterator& operator++(void); ForwardIterator operator++(int); ForwardIterator& operator--(void); `asuivre ... ForwardIterator operator--(int); } ;

enib c jt ...... 29/344 enib c jt ...... 30/344 & % & %

It´erateurs It´erateurs ' $ ' $

Exemple de d´efinition : bidirectional iterator (2) Exemple de d´efinition : bidirectional 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 ; } `asuivre ...

`asuivre ...

enib c jt ...... 31/344 enib c jt ...... 32/344 & % & % It´erateurs It´erateurs ' $ ' $

Exemple de d´efinition : bidirectional iterator (4) random access iterator

(p2 < p) -= m p += n (p1 > p) • •Z •  Z  Z -- ++Z list iterator& operator--(void) { ? ? = ? Z~ ? ? node = (list node*)((* node).previous); *p p[n] RandomAccessIterator return *this; } t1 = *p; *p = t2;

list iterator operator--(int) { list iterator tmp = *this; --*this; return tmp; RandomAccessIterator ≡ Pointer } } ; // end class list iterator ... fin

enib c jt ...... 33/344 enib c jt ...... 34/344 & % & %

It´erateurs It´erateurs ' $ ' $

random access iterator It´erateurs d´eriv´es

template template class back insert iterator class RandomAccessIterator : public random access iterator { public : : public output iterator ; T& operator*(void); template class front insert iterator const T& operator*(void) const; : public output iterator ; RandomAccessIterator& operator++(void); RandomAccessIterator operator++(int); template class insert iterator RandomAccessIterator& operator--(void); : public output iterator ; RandomAccessIterator operator--(int); RandomAccessIterator& operator+=(Distance n) ; RandomAccessIterator operator+(Distance n); template class istream iterator RandomAccessIterator& operator-=(Distance n) ; : public input iterator; RandomAccessIterator operator-(Distance n); T& operator[](Distance n); template class ostream iterator const T& operator[](Distance n) const; : public output iterator ; bool operator<(const RandomAccessIterator& iterator) const; } ;

enib c jt ...... 35/344 enib c jt ...... 36/344 & % & % It´erateurs It´erateurs ' $ ' $

It´erateurs d´eriv´es Exemple d’utilisation : vector : :iterator (1)

template v; class Reference, class Distance> v.push back(1); v.push back(2); v.push back(3) ; class reverse bidirectional iterator : public bidirectional iterator; vector : :iterator i = v.end(); template class reverse iterator i -= 2; : public random access iterator; cout << "first element is " << *i << endl;

template class raw storage iterator : public output iterator ;

enib c jt ...... 37/344 enib c jt ...... 38/344 & % & %

It´erateurs It´erateurs ' $ ' $

Exemple d’utilisation : vector : :iterator (2) Exemple d’utilisation : ostream iterator

vector v; int array[] = { 1, 5, 2, 4 } ; v.push back("hello "); char* string = "hello"; v.push back("world!") ; ostream iterator it1(cout); vector : :iterator i; copy(string, string + 5, it1); for(i = v.begin(); i!= v.end(); i++) cout << *i << endl; cout << endl;

ostream iterator it2(cout); copy(array, array + 4, it2); cout << endl;

enib c jt ...... 39/344 enib c jt ...... 40/344 & % & % It´erateurs Standard Template Library ' $ ' $

Exemple d’utilisation : istream iterator Les classes de fonctions

int i = 0; Les classes de fonctions char buffer[100]; 1. Surd´efinition de operator() ...... 43 2. Fonctions unaires et binaires ...... 44 cin.unsetf(ios : :skipws); // Disable white-space skipping. 3. Fonctions arithm´etiques ...... 45 cout << "Please enter a string : "; 4. Fonctions logiques ...... 49 5. Pointeurs de fonctions ...... 56 istream iterator s(cin); 6. Composition de fonctions ...... 60 while(*s!= ’\n’) buffer[i++] = *s++; buffer[i]=’\0’; //Nullterminatebuffer. cout << "read : " << buffer << endl;

enib c jt ...... 41/344 enib c jt ...... 42/344 & % & %

Fonctions Fonctions ' $ ' $

Surd´efinition de operator() unary/binary function STL function.h

Fonctions unaires : unary function template struct unary function { typedef Arg argument type; Result operator()(Arg arg) const; typedef Result result type; } ;

Fonctions binaires : binary function template struct binary function { typedef Arg1 first argument type; Result operator()(Arg1 arg1, Arg2 arg2) const; typedef Arg2 second argument type; typedef Result result type; } ;

enib c jt ...... 43/344 enib c jt ...... 44/344 & % & % Fonctions Fonctions ' $ ' $

plus , minus STL function.h times , divides STL function.h

template template struct plus : binary function { struct times : binary function { T operator()(const T& x, const T& y) const { T operator()(const T& x, const T& y) const { return x + y; return x * y; } } } ; } ; template template struct minus : binary function { struct divides : binary function { T operator()(const T& x, const T& y) const { T operator()(const T& x, const T& y) const { return x - y; return x / y; } } } ; } ;

enib c jt ...... 45/344 enib c jt ...... 46/344 & % & %

Fonctions Fonctions ' $ ' $

modulus , negate STL function.h Exemple d’utilisation des fonctions arithm´etiques

template int input1[4] = { 1, 6, 11, 8 } ; struct modulus : binary function { int input2[4] = { 1, 5, 2, 3 } ; T operator()(const T& x, const T& y) const { int* first1 = &input1[0]; int* last1 = &input1[4]; return x % y; int* first2 = &input2[0]; } } ; int result = 0; template plus add; times mult; struct negate : unary function { while(first1!= last1) T operator()(const T& x) const { return -x; result = add(result, mult(*first1++,*first2++)); cout << "inner product = " << result << endl; } } ;

enib c jt ...... 47/344 enib c jt ...... 48/344 & % & % Fonctions Fonctions ' $ ' $

equal to , not equal to STL function.h greater , greater equal STL function.h

template template struct equal to : binary function { struct greater : binary function { bool operator()(const T& x, const T& y) const { bool operator()(const T& x, const T& y) const { return x == y; return x > y; } } } ; } ; template template struct not equal to : binary function { struct greater equal : binary function { bool operator()(const T& x, const T& y) const { bool operator()(const T& x, const T& y) const { return x!= y; return x >= y; } } } ; } ;

enib c jt ...... 49/344 enib c jt ...... 50/344 & % & %

Fonctions Fonctions ' $ ' $

less , less equal STL function.h ident , select1st STL projectn.h

template template struct less : binary function { struct ident : public unary function { bool operator()(const T& x, const T& y) const { const U& operator()(const T& x) const { return x < y; return x; } } } ; } ; template template struct less equal : binary function { struct select1st : public unary function { bool operator()(const T& x, const T& y) const { const U& operator()(const T& x) const { return x <= y; return x.first; } } } ; } ;

enib c jt ...... 51/344 enib c jt ...... 52/344 & % & % Fonctions Fonctions ' $ ' $

logical and , logical or STL function.h logical not STL function.h

template template struct logical and : binary function { struct logical not : unary function { bool operator()(const T& x, const T& y) const { bool operator()(const T& x) const { return x && y; return!x; } } } ; } ; template struct logical or : binary function { bool operator()(const T& x, const T& y) const { return x || y; } } ;

enib c jt ...... 53/344 enib c jt ...... 54/344 & % & %

Fonctions Fonctions ' $ ' $

Exemple d’utilisation des fonctions logiques pointer to unary function STL function.h

bool input1[7] = { 1, 0, 0, 1, 1, 1, 1 } ; template bool input2[7] = { 1, 1, 0, 0, 0, 1, 0 } ; class pointer to unary function bool output[7]; : public unary function { int n = 0; protected : Result (*ptr)(Arg); count if(input1, input1 + 7, logical not(), n); public : cout << n << endl; pointer to unary function(Result *x(Arg)) : ptr(x) {} Result operator()(Arg x) const { return ptr(x); } transform(input1, input1 + 7, input2, output, } ; logical and()) ; for(int i = 0; i < 4; i++) cout << output[i] << endl;

enib c jt ...... 55/344 enib c jt ...... 56/344 & % & % Fonctions Fonctions ' $ ' $

Exemple d’utilisation : pointer to unary function pointer to binary function STL function.h

bool even(int n) { return (n % 2) == 0; } template int array[3] = { 1, 2, 3 } ; class pointer to binary function : public binary function { int* p = find if(array, array + 3, protected : pointer to unary function(even)); Result (*ptr)(Arg1, Arg2); if(p!= array + 3) cout << *p << " is even" << endl; public : pointer to binary function(Result *x(Arg1, Arg2)) : ptr(x) {} Result operator()(Arg1 x, Arg2 y) const { return ptr(x, y); } } ;

enib c jt ...... 57/344 enib c jt ...... 58/344 & % & %

Fonctions Fonctions ' $ ' $

Exemple d’utilisation : pointer to binary function unary compose STL function.h

int sum(int x, int y) { return x + y; } template class unary compose int input1[4] = { 7, 2, 3, 5 } ; : public unary function { int output[4]; protected : Operation1 op1; Operation2 op2; transform(input1, input1 + 4, input2, output, public : pointer to binary function(sum)); unary compose(const Operation1& x, const Operation2& y) for(int i = 0; i < 4; i++) cout << output[i] << endl; : op1(x), op2(y) {} result type operator()(const argument type& x) const { return op1(op2(x)); } } ;

enib c jt ...... 59/344 enib c jt ...... 60/344 & % & % Fonctions Fonctions ' $ ' $

Utilitaire pour unary compose : compose1 Exemple d’utilisation : unary compose

template struct square root : public unary function { unary compose double operator()(double x) const { return sqrt (x); } compose1(const Oper1& op1, } ; const Oper2& op2) { return unary compose(op1, op2); int input[3] = { -1, -4, -16 } ; } int output[3];

transform(input, input + 3, output, compose1(square root(), negate())); for(int i = 0; i < 3; i++) cout << output[i] << endl;

enib c jt ...... 61/344 enib c jt ...... 62/344 & % & %

Fonctions Fonctions ' $ ' $

binary compose STL function.h Utilitaire pour binary compose : compose2

template class Operation3> binary compose class binary compose compose2(const Oper1& op1, : public unary function { const Oper3& op3) { protected : return binary compose(op1, op2, op3); Operation1 op1; Operation2 op2; Operation3 op3; } public : binary compose(const Operation1& x, const Operation2& y, const Operation3& z) : op1(x), op2(y), op3(z) { } result type operator()(const argument type& x) const { return op1(op2(x), op3(x)); } } ;

enib c jt ...... 63/344 enib c jt ...... 64/344 & % & % Fonctions Standard Template Library ' $ ' $

Exemple d’utilisation : binary compose Les conteneurs

struct odd : public unary function { Les conteneurs 1. S´equences bool operator()(int n) const { return (n % 2) == 1; } (a) vector ...... 67 } ; (b) deque ...... 74 struct positive : public unary function { (c) list ...... 81 bool operator() (int n) const { return n >= 0; } 2. Arbres de recherche } ; (a) rb ...... 90 3. Tables associatives int array[] = { -2, -1 , 0, 1, 2, 3 } ; (a) pair ...... 97 int* p = find if(array, array + 6, (b) set ...... 100 compose2(logical and(), odd(), positive())); (c) multiset ...... 108 if(p!= array + 6) cout << *p << " odd and positive" << endl; (d) map ...... 116 (e) multimap ...... 125

enib c jt ...... 65/344 enib c jt ...... 66/344 & % & %

S´equences vector ' $ ' $

vector STL vector.h D´efinitions

template class vector; typedef T value type; typedef Allocator : :pointer pointer; typedef Allocator : :pointer iterator; typedef Allocator : :const pointer const iterator ; v.begin() v.end() • • typedef Allocator : :reference reference;

? ? typedef Allocator : :const reference const reference ; typedef Allocator : :size type size type; v[0] v[1] v[4] vector v; typedef Allocator : :difference type difference type; v.front() v.back()

enib c jt ...... 67/344 enib c jt ...... 68/344 & % & % vector vector ' $ ' $

Allocations Inspecteurs

vector(void); size type size(void) const; vector(size type n, const T& value = T()); size type max size(void) const; vector(const vector& x); size type capacity(void) const; vector(const iterator first, const iterator last); bool empty(void) const; vector& operator=(const vector& x); void reserve(size type n); reference operator[](size type n); const reference operator[](size type n) const; ~vector(void); reference front(void); const reference front(void) const; reference back(void); const reference back(void) const;

enib c jt ...... 69/344 enib c jt ...... 70/344 & % & %

vector vector ' $ ' $

It´erateurs Insertions et suppressions

iterator begin(void); iterator insert(iterator position, const T& x); const iterator begin(void) const; void insert(iterator position, const iterator first, iterator end(void); const iterator last); const iterator end(void) const; void insert(iterator position, size type n, const T& x); reverse iterator rbegin(void); void push back(const T& x); const reverse iterator rbegin(void) const; reverse iterator rend(void); void erase(iterator position); const reverse iterator rend(void) const; void erase(iterator first, iterator last); void pop back(void) ;

enib c jt ...... 71/344 enib c jt ...... 72/344 & % & % vector S´equences ' $ ' $

Exemple d’utilisation de la classe vector deque STL deque.h

int array1[] = { 1, 4, 25 } ; template class deque; int array2[] = { 9, 16 } ; vector v(array1, array1 + 3); v.begin() v.end() v.insert(v.begin(), 0) ; • • v.insert(v.end(), 36); ? ? for(int i = 0; i < v.size(); i++) v[0] v[1] v[4] deque v; cout << "v[" << i << "] = " << v[i] << endl; v.front() v.back() v.insert(v.begin() + 3, array2, array2 + 2); for(i = 0; i < v.size(); i++) cout << "v[" << i << "] = " << v[i] << endl;

enib c jt ...... 73/344 enib c jt ...... 74/344 & % & %

deque deque ' $ ' $

D´efinitions Allocations

typedef T value type; deque(void) ; typedef Allocator data allocator type; deque(size type n, const T& value = T()); typedef Allocator : :pointer pointer; deque(const T* first, const T* last); typedef Allocator : :reference reference; deque(const deque& x); typedef Allocator : :const reference const reference ; deque& operator=(const deque& x); typedef Allocator : :size type size type; typedef Allocator : :difference type difference type; ~deque(void); typedef Allocator map allocator type;

class iterator; // random access iterator class const iterator ;

enib c jt ...... 75/344 enib c jt ...... 76/344 & % & % deque deque ' $ ' $

Inspecteurs It´erateurs

bool empty(void) const; iterator begin(void); size type size(void) const; const iterator begin(void) const; size type max size(void) const; iterator end(void); const iterator end(void) const; reference operator[](size type n); reverse iterator rbegin(void); const reference operator[](size type n) const; const reverse iterator rbegin(void) const; reference front(void); reverse iterator rend(void); const reference front(void) const; const reverse iterator rend(void) const; reference back(void); const reference back(void) const;

enib c jt ...... 77/344 enib c jt ...... 78/344 & % & %

deque deque ' $ ' $

Insertions et suppressions Exemple d’utilisation de la classe deque

iterator insert(iterator position, const T& x); deque d; void insert(iterator position, size type n, const T& x); void insert(iterator position, const T* first, d.push back(4); d.push back(9); d.push back(16) ; const T* last); d.push front(1) ; void push front(const T& x); for(int i = 0; i < d.size(); i++) void push back(const T& x); cout << "d[" << i << "] = " << d[i] << endl; d.pop front() ; void erase(iterator position); d[2] = 25; void erase(iterator first, iterator last); for(i = 0; i < d.size(); i++) void pop front(void) ; cout << "d[" << i << "] = " << d[i] << endl; void pop back(void) ;

enib c jt ...... 79/344 enib c jt ...... 80/344 & % & % S´equences list ' $ ' $

list STL list.h D´efinitions

template class list; typedef T value type; typedef Allocator value allocator type; typedef Allocator : :pointer pointer; typedef Allocator : :reference reference; v.begin() v.end() typedef Allocator : :const reference const reference ; • • *(++v.begin()) typedef Allocator list node allocator type; ? ? typedef Allocator : :pointer link type; •- •- • • • • list v; typedef Allocator : :size type size type; typedef Allocator : :difference type v.front() v.back() difference type;

class iterator; //bidirectional iterator class const iterator ;

enib c jt ...... 81/344 enib c jt ...... 82/344 & % & %

list list ' $ ' $

Allocations Inspecteurs

list(void); bool empty(void) const; list(size type n, const T& value = T()); size type size(void) const; list(const T* first, const T* last); size type max size(void) const; list(const list& x); list& operator=(const list& x); reference front(void); const reference front(void) const; ~list(void) ; reference back(void); const reference back(void) const;

enib c jt ...... 83/344 enib c jt ...... 84/344 & % & % list list ' $ ' $

It´erateurs Insertions

iterator begin(void); iterator insert(iterator position, const T& x); const iterator begin(void) const; void insert(iterator position, const T* first, iterator end(void); const T* last); const iterator end(void); void insert(iterator position, const iterator first, reverse iterator rbegin(void); const iterator last); const reverse iterator rbegin(void) const; void insert(iterator position, size type n, const T& x); reverse iterator rend(void); void push front(const T& x); const reverse iterator rend(void) const; void push back(const T& x);

enib c jt ...... 85/344 enib c jt ...... 86/344 & % & %

list list ' $ ' $ Suppressions Manipulations

void erase(iterator position); void swap(list& x); void erase(iterator first, iterator last); void splice(iterator position, list& x); void pop front(void) ; void splice(iterator position, list& x, iterator i); void pop back(void) ; void splice(iterator position, list& x, iterator first, void remove(const T& value); iterator last); void unique(void); void merge(list& x); void reverse(void); void sort(void);

enib c jt ...... 87/344 enib c jt ...... 88/344 & % & % list Arbres ' $ ' $

Exemple d’utilisation de la classe list rb tree STL tree.h

char array[] = { ’x’, ’l’, ’x’, ’t’, ’s’, ’s’ } ; template list str(array, array + 6); class rb tree; list : :iterator i; for(i = str.begin(); i!= str.end(); i++) cout << *i; str.reverse(); for(i = str.begin(); i!= str.end(); i++) cout << *i; str.remove(’x’); for(i = str.begin(); i!= str.end(); i++) cout << *i; str.unique(); for(i = str.begin(); i!= str.end(); i++) cout << *i; str.sort(); typedef Key key type; for(i = str.begin(); i!= str.end(); i++) cout << *i; typedef Value value type;

enib c jt ...... 89/344 enib c jt ...... 90/344 & % & %

rb tree rb tree ' $ ' $

D´efinitions Allocations

typedef Allocator : :pointer pointer; rb tree(const Compare& comp = Compare(), bool always = true); typedef Allocator : :reference reference; rb tree(const value type* first, const value type* last, typedef Allocator : :const reference const reference ; const Compare& comp = Compare(), bool always = true); typedef Allocator rb tree node allocator type; rb tree(const rb tree& x, typedef Allocator : :pointer link type; bool always = true); typedef Allocator : :size type size type; rb tree& typedef Allocator : :difference type operator=(const rb tree& x) ; difference type; ~rb tree(void) ;

enib c jt ...... 91/344 enib c jt ...... 92/344 & % & % rb tree rb tree ' $ ' $ Inspecteurs Inspecteurs

Compare key comp(void) const; iterator find(const key type& x); bool empty(void) const; const iterator find(const key type& x) const; size type size(void) const; iterator lower bound(const key type& x); size type max size(void) const; const iterator lower bound(const key type& x) const; size type count(const key type& x) const; iterator upper bound(const key type& x); const iterator upper bound(const key type& x) const; pair equal range(const key type& x); pair equal range(const key type& x) const;

enib c jt ...... 93/344 enib c jt ...... 94/344 & % & %

rb tree rb tree ' $ ' $

It´erateurs Insertions et suppressions

iterator begin(void); pair insert(const value type& x); const iterator begin(void) const; iterator insert(iterator position, const value type& x); iterator end(void); void insert(iterator first, iterator last); const iterator end(void) const; void insert(const value type* first, const value type* last); reverse iterator rbegin(void); const reverse iterator rbegin(void) const; void erase(iterator position); reverse iterator rend(void); size type erase(const key type& x); const reverse iterator rend(void) const; void erase(iterator first, iterator last); void erase(const key type* first, const key type* last);

enib c jt ...... 95/344 enib c jt ...... 96/344 & % & % Tables associatives Tables associatives ' $ ' $

Les paires pair STL pair.h

Une paire est la r´eunion de deux objets de types quelconques au template sein d’une mˆeme structure. struct pair { T1 first; T2 second; pair(const T1& a, const T2& b); } ; firstT1 a T2 b second

pair p(a,b);

enib c jt ...... 97/344 enib c jt ...... 98/344 & % & %

pair Tables associatives ' $ ' $

Exemple d’utilisation de la classe pair Les ensembles ordonn´es

pair p(1, 10.5); Un ensemble ordonn´eest une collection d’objets du mˆeme type class´es selon une relation d’ordre lin´eaire (<). cout << "p.first = " << p.first << endl; cout << "p.second = " << p.second << endl; 1. Unicit´edes ´el´ements ∀a, b ∈ E, a 6= b 2. El´ements comparables ∀a, b ∈ E, (a < b) ou bien (b < a) 3. Transitivit´e ∀a, b, c ∈ E, ((a < b) ∧ (b < c)) ⇒ (a < c)

enib c jt ...... 99/344 enib c jt ...... 100/344 & % & % Tables associatives set ' $ ' $

set STL set.h D´efinitions

typedef Key key type; template class set typedef Key value type; typedef Compare key compare ; typedef Compare value compare ; typedef rep type : :const reference reference; typedef rep type : :const reference const reference ; typedef rep type : :const iterator iterator; typedef Key key type; typedef rep type : :const iterator const iterator ; typedef Key value type; typedef rep type : :const reverse iterator reverse iterator ; typedef Compare key compare ; typedef rep type : :const reverse iterator typedef Compare value compare ; const reverse iterator ; typedef rb tree, key compare> rep type; typedef rep type : :difference type difference type;

enib c jt ...... 101/344 enib c jt ...... 102/344 & % & %

set set ' $ ' $

Allocations Inspecteurs

set(const Compare& comp = Compare()); key compare key comp(void) const; set(const value type* first, const value type* last, value compare value comp(void) const; const Compare& comp = Compare()); bool empty(void) const; set(const set& x); size type size(void) const; set& operator=(const set& x) ; size type max size(void) const; size type count(const key type& x) const; ~set(void); iterator find(const key type& x) const; iterator lower bound(const key type& x) const; iterator upper bound(const key type& x) const; pair equal range(const key type& x) const;

enib c jt ...... 103/344 enib c jt ...... 104/344 & % & % set set ' $ ' $

It´erateurs Insertions et suppressions

iterator begin(void) const; pair insert(const value type& x); iterator end(void) const; iterator insert(iterator position, const value type& x); reverse iterator rbegin(void) const; void insert(const value type* first, const value type* last); reverse iterator rend(void) const; void erase(iterator position); size type erase(const key type& x); void erase(iterator first, iterator last);

enib c jt ...... 105/344 enib c jt ...... 106/344 & % & %

set Tables associatives ' $ ' $

Exemple d’utilisation de la classe set Les tas ordonn´es

set > s; Un tas ordonn´eest un ensemble ordonn´eavec r´ep´etition ´eventuelle pair > : :const iterator, bool> p; des ´el´ements. p = s.insert(42); if(p.second) 1. Multiplicit´edes ´el´ements cout << "Inserted new element " << *(p.first) << endl; ∀a, b ∈ E, (a 6= b) ou bien (a = b) else cout << "Existing element = " << *(p.first) << endl; 2. El´ements comparables p = s.insert(42); ∀a, b ∈ E, (a < b) ou bien (a = b) ou bien (b < a) if(p.second) cout << "Inserted new element" << endl; else cout << "Existing element" << endl; 3. Transitivit´e ∀a, b, c ∈ E, ((a < b) ∧ (b < c)) ⇒ (a < c)

enib c jt ...... 107/344 enib c jt ...... 108/344 & % & % Tables associatives multiset ' $ ' $

multiset STL multiset.h D´efinitions

template class multiset; typedef rep type : :size type size type; typedef rep type : :difference type difference type; typedef rep type : :const reference reference; typedef rep type : :const reference const reference ; typedef rep type : :const iterator iterator; typedef rep type : :const iterator const iterator ; typedef rep type : :const reverse iterator reverse iterator ; typedef Key key type; typedef rep type : :const reverse iterator typedef Key value type; const reverse iterator ; typedef Compare key compare ; typedef Compare value compare ; typedef rb tree, key compare> rep type;

enib c jt ...... 109/344 enib c jt ...... 110/344 & % & %

multiset multiset ' $ ' $

Allocations Inspecteurs

multiset(const Compare& comp = Compare()); key compare key comp(void) const; multiset(const value type* first, const value type* last, value compare value comp(void) const; const Compare& comp = Compare()); bool empty(void) const; multiset(const multiset& x); size type size(void) const; multiset& size type max size(void) const; operator=(const multiset& x); iterator find(const key type& x) const; size type count(const key type& x) const; iterator lower bound(const key type& x) const; iterator upper bound(const key type& x) const; pair equal range(const key type& x) const;

enib c jt ...... 111/344 enib c jt ...... 112/344 & % & % multiset multiset ' $ ' $

It´erateurs Insertions et suppressions

iterator begin(void) const; iterator insert(const value type& x); iterator end(void) const; iterator insert(iterator position, const value type& x); reverse iterator rbegin(void) const; void insert(const value type* first, const value type* last); reverse iterator rend(void) const; void erase(iterator position); size type erase(const key type& x); void erase(iterator first, iterator last);

enib c jt ...... 113/344 enib c jt ...... 114/344 & % & %

multiset Tables associatives ' $ ' $

Exemple d’utilisation de la classe multiset Les dictionnaires

// bool less than(int a, int b) { return a < b; } Un dictionnaire est un ensemble ordonn´ede cl´es (les mots du dic- // bool greater than(int a, int b) { return a > b; } tionnaire) auxquelles sont associ´ees des valeurs (les d´efinitions du dictionnaire). typedef pointer to binary function fn type; typedef multiset mset; 1. Unicit´edes cl´es int array[] = { 3, 6, 1, 9 } ; fn type f1(less than), f2(greater than); 2. Association cl´e–valeur : une cl´e, une valeur mset s1(array, array + 4, f1), s2(array, array + 4, f2);

mset : :const iterator i = s1.begin(); while(i!= s1.end()) cout << *i++ << endl; i = s2.begin(); while(i!= s2.end()) cout << *i++ << endl; enib c jt ...... 115/344 enib c jt ...... 116/344 & % & % Tables associatives map ' $ ' $

map STL map.h D´efinitions

template class map typedef rep type : :pointer pointer; typedef rep type : :reference reference; typedef rep type : :const reference const reference ; typedef rep type : :iterator iterator; typedef rep type : :const iterator const iterator ; typedef rep type : :reverse iterator reverse iterator ; typedef rep type : :const reverse iterator const reverse iterator ; typedef Key key type; typedef rep type : :size type size type; typedef pair value type; typedef rep type : :difference type difference type; typedef Compare key compare ; typedef rb tree, key compare> rep type;

enib c jt ...... 117/344 enib c jt ...... 118/344 & % & %

map map ' $ ' $

Allocations Inspecteurs

map(const Compare& comp = Compare()); key compare key comp(void) const; map(const value type* first, const value type* last, value compare value comp(void) const; const Compare& comp = Compare()); bool empty(void) const; map(const map& x); size type size(void) const; map& operator=(const map& x); size type max size(void) const; size type count(const key type& x) const; Allocator : :reference operator[](const key type& k);

enib c jt ...... 119/344 enib c jt ...... 120/344 & % & % map map ' $ ' $

Inspecteurs It´erateurs

iterator find(const key type& x); iterator begin(void); const iterator find(const key type& x) const; const iterator begin(void) const; iterator lower bound(const key type& x); iterator end(void); const iterator lower bound(const key type& x) const; const iterator end(void) const; iterator upper bound(const key type& x); reverse iterator rbegin(void); const iterator upper bound(const key type& x) const; const reverse iterator rbegin(void) const; pair equal range(const key type& x); reverse iterator rend(void); pair const reverse iterator rend(void) const; equal range(const key type& x) const;

enib c jt ...... 121/344 enib c jt ...... 122/344 & % & %

map map ' $ ' $

Insertions et suppressions Exemple d’utilisation de la classe map

pair insert(const value type& x); map > m; iterator insert(iterator position, const value type& x); m[’l’] = 50; m[’x’] = 20; m[’v’] = 5; m[’i’] = 1; void insert(const value type* first, const value type* last); cout << m[’x’] << endl; m[’x’] = 10; cout << m[’x’] << endl; cout << "m[’z’] = " << m[’z’] << endl; void erase(iterator position); cout << "m.count(’z’) = " << m.count(’z’) << endl; size type erase(const key type& x); void erase(iterator first, iterator last); pair p; p = m.insert(pair(’c’, 100)); if(p.second) cout << "First insertion successful" << endl; p = m.insert(pair(’c’, 100)); if(p.second) cout << "Second insertion successful" << endl ; else cout << (*(p.first)).first << ’,’ << (*(p.first)).second << endl;

enib c jt ...... 123/344 enib c jt ...... 124/344 & % & % Tables associatives Tables associatives ' $ ' $

Les encyclop´edies multimap STL multimap.h

Une encyclop´edie est un tas ordonn´ede cl´es (les entr´ees de l’ency- template class multimap ; clop´edie) auxquelles sont associ´ees des valeurs (les informations de l’encyclop´edie). 1. Multiplicit´edes cl´es 2. Association cl´e–valeur : une cl´e, une valeur

typedef Key key type; typedef pair value type; typedef Compare key compare ; typedef rb tree, key compare> rep type;

enib c jt ...... 125/344 enib c jt ...... 126/344 & % & %

multimap multimap ' $ ' $

D´efinitions Allocations

typedef rep type : :size type size type; multimap(const Compare& comp = Compare()); typedef rep type : :difference type difference type; multimap(const value type* first, const value type* last, typedef rep type : :reference reference; const Compare& comp = Compare()); typedef rep type : :const reference const reference ; multimap(const multimap& x); typedef rep type : :iterator iterator; multimap& typedef rep type : :const iterator const iterator ; operator=(const multimap& x) ; typedef rep type : :reverse iterator reverse iterator ; typedef rep type : :const reverse iterator const reverse iterator ;

enib c jt ...... 127/344 enib c jt ...... 128/344 & % & % multimap multimap ' $ ' $ Inspecteurs Inspecteurs

key compare key comp(void) const; iterator find(const key type& x); value compare value comp(void) const; const iterator find(const key type& x) const; bool empty(void) const; size type count(const key type& x) const; size type size(void) const; iterator lower bound(const key type& x); size type max size(void) const; const iterator lower bound(const key type& x) const; iterator upper bound(const key type& x); const iterator upper bound(const key type& x) const; pair equal range(const key type& x); pair equal range(const key type& x) const;

enib c jt ...... 129/344 enib c jt ...... 130/344 & % & %

multimap multimap ' $ ' $

It´erateurs Insertions et suppressions

iterator begin(void); iterator insert(const value type& x); const iterator begin(void) const; iterator insert(iterator position, const value type& x); iterator end(void); void insert(const value type* first, const value type* last); const iterator end(void) const; reverse iterator rbegin(void); void erase(iterator position); const reverse iterator rbegin(void) const; size type erase(const key type& x); reverse iterator rend(void); void erase(iterator first, iterator last); const reverse iterator rend(void) const;

enib c jt ...... 131/344 enib c jt ...... 132/344 & % & % multimap Standard Template Library ' $ ' $

Exemple d’utilisation de la classe multimap Adaptateurs

multimap > m; Les adaptateurs cout << m.count (’X’) << endl; 1. stack ...... 136 m.insert(pair(’X’, 10)); 2. queue ...... 139 cout << m.count(’X’) << endl; 3. priority queue ...... 142 m.insert(’X’, 20); cout << m.count(’X’) << endl; m.insert(’Y’, 32); multimap > : :iterator i = m.find(’X’) ; while(i!= m.end()) cout << (*i).first << ’,’ << (*i++).second << endl; cout << "Erased " << m.erase(’X’) << " items" << endl;

enib c jt ...... 133/344 enib c jt ...... 134/344 & % & %

Adaptateurs Adaptateurs ' $ ' $

Mˆeme impl´ementation, interfaces diff´erentes stack STL stack.h

container  P  PP  PP  PP  PP  PP Last In, First Out  PP  PP  PP stack< container > queue< container >

priority queue< container > template class stack;

typedef Container : :value type value type; typedef Container : :size type size type;

enib c jt ...... 135/344 enib c jt ...... 136/344 & % & % stack stack ' $ ' $

Interface publique Exemple d’utilisation : stack

bool empty(void) const; stack > s; size type size(void) const; value type& top(void); s.push(42); s.push(101); s.push(69); const value type& top(void) const; void push(const value type& x); while( !s.empty()) { void pop(void); cout << s.top() << endl; s.pop() ; friend bool operator==(const stack& x, } const stack& y); friend bool operator<(const stack& x, const stack& y);

enib c jt ...... 137/344 enib c jt ...... 138/344 & % & %

Adaptateurs queue ' $ ' $

queue STL stack.h Interface publique

First In, First Out bool empty(void) const; size type size(void) const; value type& front(void); const value type& front(void) const; value type& back(void); template class queue; const value type& back(void) const; void push(const value type& x); void pop(void);

friend bool operator==(const queue& x, typedef Container : :value type value type; const queue& y); typedef Container : :size type size type; friend bool operator<(const queue& x, const queue& y); enib c jt ...... 139/344 enib c jt ...... 140/344 & % & % queue Adaptateurs ' $ ' $

Exemple d’utilisation : queue priority queue STL stack.h

queue > q;

q.push(42); q.push(101); q.push(69);

while( !q.empty()) { cout << q.front() << endl; template class priority queue; q.pop() ; }

typedef Container : :value type value type; typedef Container : :size type size type;

enib c jt ...... 141/344 enib c jt ...... 142/344 & % & %

priority queue priority queue ' $ ' $

Interface publique Exemple d’utilisation : priority queue

priority queue(const Compare& x = Compare()); priority queue, less > q; priority queue(const value type* first, const value type* last, const Compare& x = Compare()); q.push(42); q.push(101); q.push(69); bool empty(void) const; size type size(void) const; while( !q.empty()) { value type& top(void); cout << q.top() << endl; const value type& top(void) const; q.pop() ; void push(const value type& x); } void pop(void);

enib c jt ...... 143/344 enib c jt ...... 144/344 & % & % Standard Template Library Calculs ' $ ' $

Les algorithmes g´en´eriques Accumulateur

Les algorithmes g´en´eriques int array[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; 1. Calculs ...... 146 int* pointer = &array[3]; 2. Comparaisons ...... 170 int n0 = 2, n = n0; 3. Manipulations ...... 206 4. Allocations ...... 244 while(pointer!= &array[8]) n += *pointer++; 5. Ensembles ...... 264 6. Recherches ...... 280 if(pointer == &array[8]) cout << (n - n0) << endl;

enib c jt ...... 145/344 enib c jt ...... 146/344 & % & %

Calculs Calculs ' $ ' $

accumulate (1) STL algo.h Exemple d’utilisation : accumulate (1)

template vector v(5); T for(int i = 0; i < v.size(); i++) v[i] = i; accumulate(InputIterator first, InputIterator last, T init) { while(first!= last) init = init + *first++; cout << accumulate(v.begin(), v.end(), 0) << endl; return init; }

enib c jt ...... 147/344 enib c jt ...... 148/344 & % & % Calculs Calculs ' $ ' $

Accumulateur g´en´eralis´e accumulate (2) STL algo.h

/* int plus(int x, int y) { return x + y; } */ template int array[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; T int* pointer = &array[3]; accumulate(InputIterator first, InputIterator last, T init, int n0 = 2, n = n0; BinaryOperation operation) { int (*function)(int,int) = plus; while(first!= last) init = operation(init, *first++); return init; while(pointer!= &array[8]) n = function(n,*pointer++); }

if(pointer == &array[8]) cout << (n - n0) << endl;

enib c jt ...... 149/344 enib c jt ...... 150/344 & % & %

Calculs Calculs ' $ ' $

Exemple d’utilisation : accumulate (2) Produit scalaire

/* int mult(int x, int y) { return x * y; } */ int array1[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; int array2[] = { 6, 8, 12, 12, 14, 17, 6, 1, 3, 6 } ; vector v(5); int* pointer1 = &array1[3]; for(int i = 0; i < v.size(); i++) v[i] = i + 1; int* pointer2 = &array2[1]; int n0 = 2, n = n0; cout << accumulate(v.begin(), v.end(), 1, mult) << endl; while(pointer1!= &array1[8]) n += *pointer1++ * *pointer2++;

if(pointer1 == &array1[8]) cout << (n - n0) << endl;

enib c jt ...... 151/344 enib c jt ...... 152/344 & % & % Calculs Calculs ' $ ' $

inner product (1) STL algo.h Exemple d’utilisation : inner product (1)

template v1(5), v2(5); class T> for(int i = 0; i < v1.size(); i++) v1[i] = i; T for(int j = 0; j < v2.size(); j++) v2[j] = j; inner product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init) { cout << inner product(v1.begin(), v1.end(), v2.begin(), 0) while(first1!= last1) << endl; init = init + (*first1++ * *first2++); return init; }

enib c jt ...... 153/344 enib c jt ...... 154/344 & % & %

Calculs Calculs ' $ ' $

Produit scalaire g´en´eralis´e inner product (2) STL algo.h

/* int plus(int x, int y) { return x + y; } */ template int array1[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; T int array2[] = { 6, 8, 12, 12, 14, 17, 6, 1, 3, 6 } ; inner product(InputIterator1 first1, InputIterator1 last1, int *pointer1 = &array1[3], *pointer2 = &array2[1]; InputIterator2 first2, T init, int n0 = 2, n = n0; BinaryOperation1 op1, BinaryOperation2 op2) { int (*function1)(int,int) = plus; while(first1!= last1) int (*function2)(int,int) = times; init = op1(init, op2(*first1++, *first2++)); return init; while(pointer1!= &array1[8]) } n = function1(n,function2(*pointer1++,*pointer2++));

enib c jt ...... 155/344 enib c jt ...... 156/344 & % & % Calculs Calculs ' $ ' $

Exemple d’utilisation : inner product (2) Somme partielle

/* int plus(int x, int y) { return x + y; } */ int array1[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } ; /* int times(int x, int y) { return x * y; } */ int array2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ; int* pointer = &array1[0]; vector v1(5), v2(5); int* result = &array2[0]; for(int i = 0; i < v1.size(); i++) v1[i] = i; for(int j = 0; j < v2.size(); j++) v2[j] = j; int value = *result = *pointer; while(++pointer!= &array1[8]) { cout << inner product(v1.begin(), v1.end(), v2.begin(), 0, value += *pointer; *++result = value; plus, mult) } << endl; result = &array2[0]; while(result!= &array2[10]) cout << *result++ << endl;

enib c jt ...... 157/344 enib c jt ...... 158/344 & % & %

Calculs Calculs ' $ ' $

partial sum (1) STL algo.h Exemple d’utilisation : partial sum (1)

template vector v1(5); OutputIterator for(int i = 0; i < v1.size(); i++) v1[i] = i; partial sum(InputIterator first, InputIterator last, vector v2(v1.size()); OutputIterator result) { if(first == last) return result; partial sum(v1.begin(), v1.end(), v2.begin()); T value = *result = *first; while(++first!= last) { ostream iterator iter(cout," "); value = value + *first; copy(v1.begin(), v1.end(), iter); cout << endl; *++result = value; copy(v2.begin(), v2.end(), iter); cout << endl; } return ++result; }

enib c jt ...... 159/344 enib c jt ...... 160/344 & % & % Calculs Calculs ' $ ' $

Somme partielle g´en´eralis´ee partial sum (2) STL algo.h

/* int plus(int x, int y) { return x + y; } */ template int array1[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } ; OutputIterator int array2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ; partial sum(InputIterator first, InputIterator last, int* pointer = &array1[0]; int* result = &array2[0]; OutputIterator result, BinaryOperation operation) { int (*operation)(int,int) = plus; if(first == last) return result; T value = *result = *first; int value = *result = *pointer; while(++first!= last) { while(++pointer!= &array1[8]) { value = operation(value,*first); value = operation(value,*pointer); *++result = value; *++result = value; } } return ++result; }

enib c jt ...... 161/344 enib c jt ...... 162/344 & % & %

Calculs Calculs ' $ ' $

Exemple d’utilisation : partial sum (2) Diff´erences

vector v1(5); int array1[] = { 1, 2, 5, 3, 8 } ; for(int i = 0; i < v1.size(); i++) v1[i] = i; int array2[] = { 0, 0, 0, 0, 0 } ; vector v2(v1.size()); int* pointer = &array1[0]; int* result = &array2[0];

partial sum(v1.begin(), v1.end(), v2.begin(), times()); int value = *result = *pointer; while(++pointer!= &array1[5]) { ostream iterator iter(cout," "); int tmp = *pointer; copy(v1.begin(), v1.end(), iter); cout << endl; *++result = tmp - value; copy(v2.begin(), v2.end(), iter); cout << endl; value = tmp; }

for(int i = 0; i < 5; i++) cout << array2[i] << endl;

enib c jt ...... 163/344 enib c jt ...... 164/344 & % & % Calculs Calculs ' $ ' $

adjacent difference (1) STL algo.h Exemple d’utilisation : adjacent difference (1)

template vector v1(10); OutputIterator for(int i = 0; i < v1.size(); i++) v1[i] = i + 1; adjacent difference(InputIterator first, InputIterator last, vector v2(v1.size()); OutputIterator result) { if(first == last) return result; adjacent difference(v1.begin(), v1.end(), v2.begin()); T value = *result = *first; while(++first!= last) { ostream iterator iter(cout, " "); T tmp = *first; copy(v1.begin(), v1.end(), iter); cout << endl; *++result = tmp - value; copy(v2.begin(), v2.end(), iter); cout << endl; value = tmp; } return ++result; }

enib c jt ...... 165/344 enib c jt ...... 166/344 & % & %

Calculs Calculs ' $ ' $

Diff´erences g´en´eralis´ees adjacent difference (2) STL algo.h

/* int moins(int x, int y) { return x - y; } */ template int array1[] = { 1, 2, 5, 3, 8 } ; OutputIterator int array2[] = { 0, 0, 0, 0, 0 } ; adjacent difference(InputIterator first, InputIterator last, int* pointer = &array1[0]; int* result = &array2[0]; OutputIterator result, BinaryOperation operation) { int (*operation)(int,int) = moins; if(first == last) return result; T value = *result = *first; int value = *result = *pointer; while(++first!= last) { while(++pointer!= &array1[5]) { T tmp = *first; *++result = operation(tmp,value); int tmp = *pointer; value = tmp; *++result = operation(tmp,value); } value = tmp; return ++result; } } enib c jt ...... 167/344 enib c jt ...... 168/344 & % & % Calculs Comparaisons ' $ ' $

Exemple d’utilisation : adjacent difference (2) != , > , <= , >= STL function.h

/* int mult(int a, int b) { return a * b; } */ template bool operator!=(const T& x, const T& y) { return!(x == y); } vector v1(10); for(int i = 0; i < v1.size(); i++) v1[i] = i + 1; template vector v2(v1.size()); bool operator>(const T& x, const T& y) { return y < x; }

adjacent difference(v1.begin(), v1.end(), v2.begin(), mult); template ostream iterator iter(cout, " "); bool operator<=(const T& x, const T& y) { return!(y < x); } copy(v1.begin(), v1.end(), iter); cout << endl; copy(v2.begin(), v2.end(), iter); cout << endl; template bool operator>=(const T& x, const T& y) { return!(x < y); }

enib c jt ...... 169/344 enib c jt ...... 170/344 & % & %

Comparaisons Comparaisons ' $ ' $

Exemple d’utilisation : != , > , <= , >= Maximum de 2 ´el´ements

/* int less(int x, int y) { return x < y; } */

int x, y; int max1, max2; int (*compare)(int,int) = less;

max1 = x < y? y : x; max2 = compare(x,y)? y : x;

enib c jt ...... 171/344 enib c jt ...... 172/344 & % & % Comparaisons Comparaisons ' $ ' $

max (1),(2) STL algobase.h Exemple d’utilisation : max (1),(2)

template const T& max(const T& x, const T& y) { return x < y? y : x; }

template const T& max(const T& x, const T& b, Compare compare) { return compare(x, y)? y : x; }

enib c jt ...... 173/344 enib c jt ...... 174/344 & % & %

Comparaisons Comparaisons ' $ ' $

Maximum de n ´el´ements max element (1) STL algo.h

/* int less(int x, int y) { return x < y; } */ template InputIterator int array[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; max element(InputIterator first, InputIterator last) { int* pointer = &array[0]; if (first == last) return first; int max1 = *pointer, max2 = *pointer; InputIterator result = first; int (*compare)(int,int) = less; while(++first!= last) if(*result < *first) result = first; while(++pointer!= &array[8]) { return result; if(max1 < *pointer) max1 = *pointer; } if(compare(max2,*pointer) max2 = *pointer; }

enib c jt ...... 175/344 enib c jt ...... 176/344 & % & % Comparaisons Comparaisons ' $ ' $

Exemple d’utilisation : max element (1) max element (2) STL algo.h

template InputIterator max element(InputIterator first, InputIterator last, Compare compare) { if (first == last) return first; InputIterator result = first; while(++first!= last) if(compare(*result,*first)) result = first; return result; }

enib c jt ...... 177/344 enib c jt ...... 178/344 & % & %

Comparaisons Comparaisons ' $ ' $

Exemple d’utilisation : max element (2) Minimum de 2 ´el´ements

/* int less(int x, int y) { return x < y; } */

int x, y; int min1, min2; int (*compare)(int,int) = less;

min1 = x < y? x : y; min2 = compare(x,y)? x : y;

enib c jt ...... 179/344 enib c jt ...... 180/344 & % & % Comparaisons Comparaisons ' $ ' $

min (1),(2) STL algobase.h Exemple d’utilisation : min (1),(2)

template const T& min(const T& x, const T& y) { return x < y? x : y; }

template const T& min(const T& x, const T& b, Compare compare) { return compare(x, y)? x : y; }

enib c jt ...... 181/344 enib c jt ...... 182/344 & % & %

Comparaisons Comparaisons ' $ ' $

Minimum de n ´el´ements min element (1) STL algo.h

/* int less(int x, int y) { return x < y; } */ template InputIterator int array[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; min element(InputIterator first, InputIterator last) { int* pointer = &array[0]; if (first == last) return first; int min1 = *pointer, min2 = *pointer; InputIterator result = first; int (*compare)(int,int) = less; while(++first!= last) if(*first < *result) result = first; while(++pointer!= &array[8]) { return result; if(*pointer < min1) min1 = *pointer; } if(compare(*pointer,min2) min2 = *pointer; }

enib c jt ...... 183/344 enib c jt ...... 184/344 & % & % Comparaisons Comparaisons ' $ ' $

Exemple d’utilisation : min element (1) min element (2) STL algo.h

template InputIterator min element(InputIterator first, InputIterator last, Compare compare) { if (first == last) return first; InputIterator result = first; while(++first!= last) if(compare(*first,*result)) result = first; return result; }

enib c jt ...... 185/344 enib c jt ...... 186/344 & % & %

Comparaisons Comparaisons ' $ ' $

Exemple d’utilisation : min element (2) Diff´erence entre 2 s´equences

int array1[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; int array2[] = { 6, 8, 12, 12, 14, 17, 6, 1, 3, 6 } ; int* pointer1 = &array1[3]; int* pointer2 = &array2[0];

while(pointer1!= &array1[8] && *pointer1 == *pointer2) { ++pointer1; ++pointer2; }

if(pointer1 == &array1[8]) cout << "no mismatch" << endl;

enib c jt ...... 187/344 enib c jt ...... 188/344 & % & % Comparaisons Comparaisons ' $ ' $

mismatch (1) STL algo.h Exemple d’utilisation : mismatch (1)

template pair mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) { while(first1!= last1 && *first1 == *first2) { ++first1 ; ++first2 ; } return pair(first1,first2); }

enib c jt ...... 189/344 enib c jt ...... 190/344 & % & %

Comparaisons Comparaisons ' $ ' $

Diff´erence g´en´eralis´ee mismatch (2) STL algo.h

/* int equal(int x, int y) { return x == y; } */ template int array1[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; pair int array2[] = { 6, 8, 12, 12, 14, 17, 6, 1, 3, 6 } ; mismatch(InputIterator1 first1, InputIterator1 last1, int* pointer1 = &array1[3]; int* pointer2 = &array2[0]; InputIterator2 first2, BinaryPredicate predicate) { int (*predicate)(int,int) = equal; while(first1!= last1 && predicate(*first1,*first2)) { ++first1 ; while(pointer1!= &array1[10] && ++first2 ; predicate(*pointer1,*pointer2)) { } ++pointer1; ++pointer2; return pair(first1,first2); } } if(pointer1 == &array1[10]) cout << "no mismatch" << endl;

enib c jt ...... 191/344 enib c jt ...... 192/344 & % & % Comparaisons Comparaisons ' $ ' $

Exemple d’utilisation : mismatch (2) Egalit´ede 2 s´equences

int array1[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; int array2[] = { 6, 8, 12, 12, 14, 17, 6, 1, 3, 6 } ; int* pointer1 = &array1[3]; int* pointer2 = &array2[0];

while(pointer1!= &array1[8] && *pointer1 == *pointer2) { ++pointer1; ++pointer2; }

if(pointer1!= &array1[8]) cout << "not equal" << endl;

enib c jt ...... 193/344 enib c jt ...... 194/344 & % & %

Comparaisons Comparaisons ' $ ' $

equal (1) STL algo.h Exemple d’utilisation : equal (1)

template bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) { return mismatch(first1,last1,first2).first == last1; }

enib c jt ...... 195/344 enib c jt ...... 196/344 & % & % Comparaisons Comparaisons ' $ ' $

Egalit´eg´en´eralis´ee equal (2) STL algo.h

/* int equal(int x, int y) { return x == y; } */ template int array1[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; bool int array2[] = { 6, 8, 12, 12, 14, 17, 6, 1, 3, 6 } ; equal(InputIterator1 first1, InputIterator1 last1, int* pointer1 = &array1[3]; int* pointer2 = &array2[0]; InputIterator2 first2, BinaryPredicate predicate) { int (*predicate)(int,int) = equal; return mismatch(first1,last1,first2,predicate).first == last1; while(pointer1!= &array1[10] && } predicate(*pointer1,*pointer2)) { ++pointer1; ++pointer2; } if(pointer1!= &array1[10]) cout << "not equal" << endl;

enib c jt ...... 197/344 enib c jt ...... 198/344 & % & %

Comparaisons Comparaisons ' $ ' $

Exemple d’utilisation : equal (2) Comparaison lexicographique de 2 s´equences

int array1[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; int array2[] = { 6, 8, 12, 12, 14, 17, 6, 1, 3, 6 } ; int* pointer1 = &array1[3]; int* pointer2 = &array2[0];

int result = -1; while(pointer1!= &array1[8] && pointer2!= &array2[9]) { if(*pointer1 < *pointer2) { result = 1; break; } if(*pointer2++ < *pointer1++) { result = 0; break; } } if(result == -1) result = (pointer1 == &array1[8] && pointer2!= &array2[9]);

enib c jt ...... 199/344 enib c jt ...... 200/344 & % & % Comparaisons Comparaisons ' $ ' $

lexicographical compare (1) STL algo.h Exemple d’utilisation : lexicographical compare (1)

template bool lexicographical compare( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2) { while(first1!= last1 && first2!= last2) { if(*first1 < *first2) return true; if(*first2++ < *first1++) return false; } return (first1 == last1 && first2!= last2;) }

enib c jt ...... 201/344 enib c jt ...... 202/344 & % & %

Comparaisons Comparaisons ' $ ' $

Comparaison lexicographique g´en´eralis´ee lexicographical compare (2) STL algo.h

/* int less(int x, int y) { return x < y; } */ template int array1[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; bool int array2[] = { 6, 8, 12, 12, 14, 17, 6, 1, 3, 6 } ; lexicographical compare( int* pointer1 = &array1[3]; int* pointer2 = &array2[0]; InputIterator1 first1, InputIterator1 last1, int (*compare)(int,int) = less; InputIterator2 first2, InputIterator2 last2, int res = -1; Compare compare) { while(pointer1!= &array1[8] && pointer2!= &array2[9]) { while(first1!= last1 && first2!= last2) { if(compare(*pointer1,*pointer2)) { res = 1; break; } if(compare(*first1,*first2)) return true; if(compare(*pointer2++,*pointer1++)) { res = 0; break; } if(compare(*first2++,*first1++)) return false ; } } if(res == -1) return (first1 == last1 && first2!= last2;) res = (pointer1 == &array1[8] && pointer2!= &array2[9]); } enib c jt ...... 203/344 enib c jt ...... 204/344 & % & % Comparaisons Algorithmes g´en´eriques ' $ ' $

Exemple d’utilisation : lexicographical compare (2) Manipulations

for each ...... 214 replace ...... 225 generate ...... 216 replace copy ...... 229 generate n ...... 219 replace copy if ..... 231 iter swap ...... 209 replace if ...... 227 partition ...... 241 swap ...... 207 remove ...... 237 swap ranges ...... 211 remove copy ...... 233 transform (1) ...... 221 remove copy if ...... 235 transform (2) ...... 223 remove if ...... 239

enib c jt ...... 205/344 enib c jt ...... 206/344 & % & %

Manipulations Manipulations ' $ ' $

swap STL algobase.h Exemple d’utilisation : swap

template int numbers[6] = { 0, 1, 2, 3, 4, 5 } ; void swap(T& a, T& b) { swap(numbers[0], numbers[3]); T tmp = a; a = b; for(int i = 0; i < 6; i++) cout << numbers[i] << ’ ’; b = tmp; }

enib c jt ...... 207/344 enib c jt ...... 208/344 & % & % Manipulations Manipulations ' $ ' $

iter swap STL algo.h Exemple d’utilisation : iter swap

template int numbers[6] = { 0, 1, 2, 3, 4, 5 } ; void iter swap(ForwardIterator1 a, ForwardIterator2 b) { iter swap(numbers, numbers + 3); iter swap(a, b, value type(a)) ; } for(int i = 0; i < 6; i++) cout << numbers[i] << ’ ’;

template void iter swap(ForwardIterator1 a, ForwardIterator2 b, T*) { T tmp = *a; *a = *b; *b = tmp; }

enib c jt ...... 209/344 enib c jt ...... 210/344 & % & %

Manipulations Manipulations ' $ ' $

swap ranges STL algo.h Exemple d’utilisation : swap ranges

template char* word1 = "world!"; ForwardIterator2 char* word2 = "Hello "; swap ranges(ForwardIterator1 first1, ForwardIterator1 last1, cout << word1 << word2 << endl; ForwardIterator2 first2) { while(first1!= last1) iter swap(first1++, first2++); swap ranges(word1, word1 + : :strlen(word1), word2); return first2; } cout << word1 << word2 << endl;

enib c jt ...... 211/344 enib c jt ...... 212/344 & % & % Manipulations Manipulations ' $ ' $

Application d’une fonction for each STL algo.h

/* void display(int x) { cout << x * x << ’ ’; } */ template Function int array[] = { 1, 3, 5, 6, 8, 12, 12, 15, 17, 20 } ; for each(InputIterator first, InputIterator last, int* first = &array[3]; Function function) { int* last = &array[8]; while(first!= last) function(*first++); void (*function)(int) = display; return function; } while(first!= last) function(*first++);

enib c jt ...... 213/344 enib c jt ...... 214/344 & % & %

Manipulations Manipulations ' $ ' $

Exemple d’utilisation : for each generate STL algo.h

/* void display(int x) { cout << x * x << ’ ’; } */ template void vector v1(10); generate(ForwardIterator first, ForwardIterator last, for(int i = 0; i < v1.size(); i++) v1[i] = i; Generator generator) { while(first!= last) *first++ = generator(); for each(v1.begin(), v1.end(), display); }

enib c jt ...... 215/344 enib c jt ...... 216/344 & % & % Manipulations Manipulations ' $ ' $

Exemple d’utilisation : generate Exemple d’utilisation : generate (suite)

class Fibonacci { vector v1(10); private : Fibonacci generator; int v1, v2; public : generate(v1.begin(), v1.end(), generator); Fibonacci(void) : v1(0), v2(1) {} int operator()(void) { ostream iterator iter(cout, " "); int r = v1 + v2; copy(v1.begin(), v1.end(), iter); v1 = v2; v2 = r; return v1; ... fin } } ; `asuivre ...

enib c jt ...... 217/344 enib c jt ...... 218/344 & % & %

Manipulations Manipulations ' $ ' $

generate n STL algo.h Exemple d’utilisation : generate n

template v1(10); class Generator> Fibonacci generator; OutputIterator generate n(OutputIterator first, Size n, generate n(v1.begin(), v1.size(), generator); Generator generator) { while(n-- > 0) *first++ = generator(); ostream iterator iter(cout, " "); return first; copy(v1.begin(), v1.end(), iter); }

enib c jt ...... 219/344 enib c jt ...... 220/344 & % & % Manipulations Manipulations ' $ ' $

transform (1) STL algo.h Exemple d’utilisation : transform (1)

template OutputIterator int numbers[6] = { -5, -1, 0, 1, 6, 11 } ; transform(InputIterator first, InputIterator last, int result[6]; OutputIterator result, UnaryOperation operation) { while(first!= last) *result++ = operation(*first++); transform(numbers, numbers + 6, result, negate int); return result; } for(int i = 0; i < 6; i++) cout << result[i] << ’ ’;

enib c jt ...... 221/344 enib c jt ...... 222/344 & % & %

Manipulations Manipulations ' $ ' $

transform (2) STL algo.h Exemple d’utilisation : transform (2)

template OutputIterator int trans[] = { transform(InputIterator1 first1, InputIterator1 last1, -4, 4, -6, -6, -10, 0, 10, -6, 6, 0, -1, -77 InputIterator2 first2, OutputIterator result, } ; BinaryOperation operation) { char n[] = "Larry Mullen"; while(first1!= last1) *result++ = operation(*first1++, *first2++); const unsigned int count = : :strlen(n); return result; ostream iterator iter(cout); } transform(n, n + count, trans, iter, map char);

enib c jt ...... 223/344 enib c jt ...... 224/344 & % & % Manipulations Manipulations ' $ ' $

replace STL algo.h Exemple d’utilisation : replace

template int numbers[6] = { 0, 1, 2, 0, 1, 2 } ; void replace(ForwardIterator first, ForwardIterator last, replace(numbers, numbers + 6, 2, 42); const T& old value, const T& new value) { while(first!= last) { for(int i = 0; i < 6; i++) cout << numbers[i] << ’ ’; if(*first == old value) *first = new value; ++first ; } }

enib c jt ...... 225/344 enib c jt ...... 226/344 & % & %

Manipulations Manipulations ' $ ' $

replace if STL algo.h Exemple d’utilisation : replace if

template /* bool odd(int a) { return a % 2; } */ void replace if(ForwardIterator first, ForwardIterator last, vector v1(10); Predicate predicate, const T& new value) { for(int i = 0; i < v1.size(); i++) { while(first!= last) { v1[i] = i % 5; cout << v1[i] << ’ ’; if(predicate(*first)) *first = new value ; } ++first ; } replace if(v1.begin(), v1.end(), odd, 42); } for(i = 0; i < v1.size(); i++) cout << v1[i] << ’ ’;

enib c jt ...... 227/344 enib c jt ...... 228/344 & % & % Manipulations Manipulations ' $ ' $

replace copy STL algo.h Exemple d’utilisation : replace copy

template int result[6] = { 0, 0, 0, 0, 0, 0 } ; OutputIterator replace copy(InputIterator first, InputIterator last, replace copy(numbers, numbers + 6, result, 2, 42); OutputIterator result, const T& old value, const T& new value) { for(int i = 0; i < 6; i++) cout << result[i] << ’ ’; while(first!= last) { *result++ = *first == old value? new value : *first; ++first ; } return result; }

enib c jt ...... 229/344 enib c jt ...... 230/344 & % & %

Manipulations Manipulations ' $ ' $

replace copy if STL algo.h Exemple d’utilisation : replace copy if

template OutputIterator vector v1(10), v2(10); replace copy if(Iterator first, Iterator last, for(int i = 0; i < v1.size(); i++) v1[i] = i % 5; OutputIterator result, Predicate predicate, ostream iterator iter(cout, " "); const T& new value) { copy(v1.begin(), v1.end(), iter); while(first!= last) { *result++ = predicate(*first)? new value : *first; replace copy if(v1.begin(), v1.end(), v2.begin(), odd, 42); ++first ; } copy(v1.begin(), v1.end(), iter); return result; copy(v2.begin(), v2.end(), iter); }

enib c jt ...... 231/344 enib c jt ...... 232/344 & % & % Manipulations Manipulations ' $ ' $

remove copy STL algo.h Exemple d’utilisation : remove copy

template int result[6] = { 0, 0, 0, 0, 0, 0 } ; OutputIterator remove copy(InputIterator first, InputIterator last, remove copy(numbers, numbers + 6, result, 2); OutputIterator result, const T& value) { while(first!= last) { for(int i = 0; i < 6; i++) cout << result[i] << ’ ’; if(*first!= value) *result++ = *first; ++first ; } return result; }

enib c jt ...... 233/344 enib c jt ...... 234/344 & % & %

Manipulations Manipulations ' $ ' $

remove copy if STL algo.h Exemple d’utilisation : remove copy if

template OutputIterator int numbers[6] = { 1, 2, 3, 1, 2, 3 } ; remove copy if(InputIterator first, InputIterator last, int result[6] = { 0, 0, 0, 0, 0, 0 } ; OutputIterator result, Predicate predicate) { while(first!= last) { remove copy if(numbers, numbers + 6, result, odd); if(!predicate(*first)) *result++ = *first; ++first ; for(int i = 0; i < 6; i++) cout << result[i] << ’ ’; } return result; }

enib c jt ...... 235/344 enib c jt ...... 236/344 & % & % Manipulations Manipulations ' $ ' $

remove STL algo.h Exemple d’utilisation : remove

template int numbers[6] = { 1, 2, 3, 1, 2, 3 } ; ForwardIterator remove(ForwardIterator first, ForwardIterator last, remove(numbers, numbers + 6, 1); const T& value) { first = find(first, last, value); for(int i = 0; i < 6; i++) cout << numbers[i] << ’ ’; ForwardIterator next = first; return first == last? first : remove copy(++next, last, first, value); }

enib c jt ...... 237/344 enib c jt ...... 238/344 & % & %

Manipulations Manipulations ' $ ' $

remove if STL algo.h Exemple d’utilisation : remove if

template /* bool odd(int a) { return a % 2; } */ ForwardIterator remove if(ForwardIterator first, ForwardIterator last, int numbers[6] = { 0, 0, 1, 1, 2, 2 } ; Predicate predicate) { first = find if(first, last, predicate); remove if(numbers, numbers + 6, odd); ForwardIterator next = first; return first == last? first : for(int i = 0; i < 6; i++) cout << numbers[i] << ’ ’; remove copy if(++next, last, first, predicate); }

enib c jt ...... 239/344 enib c jt ...... 240/344 & % & % Manipulations Manipulations ' $ ' $

partition STL algo.h partition (suite) STL algo.h

template --last ; BidirectionalIterator while(true) { partition(BidirectionalIterator first, if(first == last) return first; BidirectionalIterator last, Predicate predicate) { else if(!predicate(*last)) --last; else break; while(true) { } while(true) { iter swap(first, last); ++first; if(first == last) return first; } else if(predicate(*first)) ++first; else break; } } ... fin `asuivre ...

enib c jt ...... 241/344 enib c jt ...... 242/344 & % & %

Manipulations Allocations ' $ ' $

Exemple d’utilisation : partition construct STL defalloc.h

/* int less 10(int a) { return a < 10? 1 : 0; } */ template void int numbers[6] = { 6, 12, 3, 10, 1, 20 } ; construct(T1* p, const T2& value) { new(p) T1(value); partition(numbers, numbers + 6, less 10); }

for(int i = 0; i < 6; i++) cout << numbers[i] << ’ ’;

enib c jt ...... 243/344 enib c jt ...... 244/344 & % & % Allocations Allocations ' $ ' $

Exemple d’utilisation : construct destroy (1) STL defalloc.h

template void destroy(T* pointer) { pointer->~T(); }

enib c jt ...... 245/344 enib c jt ...... 246/344 & % & %

Allocations Allocations ' $ ' $

Exemple d’utilisation : destroy (1) destroy (2) STL algobase.h

template void destroy(ForwardIterator first, ForwardIterator last) { while (first!= last) destroy(first++); }

enib c jt ...... 247/344 enib c jt ...... 248/344 & % & % Allocations Allocations ' $ ' $

Exemple d’utilisation : destroy (2) fill STL algo.h

template void fill(ForwardIterator first, ForwardIterator last, const T& value) { while (first!= last) *first++ = value; }

enib c jt ...... 249/344 enib c jt ...... 250/344 & % & %

Allocations Allocations ' $ ' $

Exemple d’utilisation : fill fill n STL algobase.h

template void fill n(OutputIterator first, Size n, const T& value) { while (n-- > 0) *first++ = value; }

enib c jt ...... 251/344 enib c jt ...... 252/344 & % & % Allocations Allocations ' $ ' $

Exemple d’utilisation : fill n uninitialized fill STL algobase.h

template void uninitialized fill(ForwardIterator first, ForwardIterator last, const T& x) { while (first!= last) construct(first++, x); }

enib c jt ...... 253/344 enib c jt ...... 254/344 & % & %

Allocations Allocations ' $ ' $

Exemple d’utilisation : uninitialized fill uninitialized fill n STL algobase.h

template void uninitialized fill n(ForwardIterator first, Size n, const T& x) { while (n--) construct(first++, x); }

enib c jt ...... 255/344 enib c jt ...... 256/344 & % & % Allocations Allocations ' $ ' $

Exemple d’utilisation : uninitialized fill n copy STL algobase.h

template OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result) { while (first!= last) *result++ = *first++; return result; }

enib c jt ...... 257/344 enib c jt ...... 258/344 & % & %

Allocations Allocations ' $ ' $

Exemple d’utilisation : copy copy backward STL algo.h

template BidirectionalIterator2 copy backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result) { while (first!= last) *--result = *--last; return result; }

enib c jt ...... 259/344 enib c jt ...... 260/344 & % & % Allocations Allocations ' $ ' $

Exemple d’utilisation : copy backward uninitialized copy STL algobase.h

template ForwardIterator uninitialized copy(InputIterator first, InputIterator last, ForwardIterator result) { while (first!= last) construct(result++, *first++); return result; }

enib c jt ...... 261/344 enib c jt ...... 262/344 & % & %

Allocations Ensembles ' $ ' $

Exemple d’utilisation : uninitialized copy includes (1) STL algo.h

template bool includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2) { while(first1!= last1 && first2!= last2) if(*first2 < *first1) return false; else if(*first1 < *first2) ++first1; else (++first1, ++first2); return first2 == last2; }

enib c jt ...... 263/344 enib c jt ...... 264/344 & % & % Ensembles Ensembles ' $ ' $

Exemple d’utilisation : includes (1) includes (2) STL algo.h

vector v1(10), v2(3); template for(int i = 0; i < v1.size(); i++) v1[i] = i; bool if(includes(v1.begin(), v1.end(), v2.begin(), v2.end())) includes(InputIterator1 first1, InputIterator1 last1, cout << "v1 includes v2" << endl; InputIterator2 first2, InputIterator2 last2, else cout << "v1 does not include v2" << endl; Compare compare) { while(first1!= last1 && first2!= last2) for(i = 0; i < v2.size(); i++) v2[i] = i + 3; if(compare(*first2, *first1)) return false; if(includes(v1.begin(), v1.end(), v2.begin(), v2.end())) else if(compare(*first1, *first2)) ++first1; cout << "v1 includes v2" << endl; else (++first1, ++first2); else cout << "v1 does not include v2" << endl; return first2 == last2; }

enib c jt ...... 265/344 enib c jt ...... 266/344 & % & %

Ensembles Ensembles ' $ ' $

Exemple d’utilisation : includes (2) set union (1) STL algo.h

bool compare strings(const char* s1, const char* s2) { template } OutputIterator set union(InputIterator1 first1, InputIterator1 last1, char* names[] = { "Alexis", "Dany", "Serge", "Vincent" } ; InputIterator2 first2, InputIterator2 last2, vector v1(4), v2(2); OutputIterator result) { for(int i = 0; i < v1.size(); i++) v1[i] = names[i]; while(first1!= last1 && first2!= last2) v2[0] = "Dany"; v2[1] = "Vincent"; if(*first1 < *first2) *result++ = *first1++; bool inc = includes(v1.begin(), v1.end(), v2.begin(), else if(*first2 < *first1) *result++ = *first2++; v2.end(), compare strings) ; else { *result++ = *first1++; first2++; } if(inc) cout << "v1 includes v2" << endl; return copy(first2, last2, copy(first1, last1, result)); else cout << "v1 does not include v2" << endl; }

enib c jt ...... 267/344 enib c jt ...... 268/344 & % & % Ensembles Ensembles ' $ ' $

Exemple d’utilisation : set union (1) set union (2) STL algo.h

int v1[3] = { 13, 18, 23 } ; template int result[7] = { 0, 0, 0, 0, 0, 0, 0 } ; OutputIterator set union(InputIterator1 first1, InputIterator1 last1, set union(v1, v1 + 3, v2, v2 + 4, result); InputIterator2 first2, InputIterator2 last2, for(int i = 0; i < 7; i++) cout << result[i] << ’ ’; OutputIterator result, Compare compare) { cout << endl; while(first1!= last1 && first2!= last2) if(compare(*first1,*first2)) *result++ = *first1++; else if(compare(*first2,*first1)) *result++ = *first2++; else { *result++ = *first1++; first2++; } return copy(first2, last2, copy(first1, last1, result)); }

enib c jt ...... 269/344 enib c jt ...... 270/344 & % & %

Ensembles Ensembles ' $ ' $

Exemple d’utilisation : set union (2) set intersection (1) STL algo.h

char* word1 = "ABCDEFGHIJKLMNO"; template ostream iterator iter(cout, " "); OutputIterator set intersection( cout << "word1 : "; InputIterator1 first1, InputIterator1 last1, copy(word1, word1 + : :strlen(word1), iter); InputIterator2 first2, InputIterator2 last2, cout << "\nword2 : "; OutputIterator result) { copy(word2, word2 + : :strlen(word2), iter); while(first1!= last1 && first2!= last2) if(*first1 < *first2) ++first1; set union(word1, word1 + : :strlen(word1), word2, else if(*first2 < *first1) ++first2; word2 + : :strlen(word2), iter, less()); else { *result++ = *first1++; ++first2; } cout << endl; return result; }

enib c jt ...... 271/344 enib c jt ...... 272/344 & % & % Ensembles Ensembles ' $ ' $

Exemple d’utilisation : set intersection (1) set intersection (2) STL algo.h

int v1[3] = { 13, 18, 23 } ; template int result[4] = { 0, 0, 0, 0 } ; OutputIterator set intersection( set intersection(v1, v1 + 3, v2, v2 + 4, result); InputIterator1 first1, InputIterator1 last1, for(int i = 0; i < 4; i++) cout << result[i] << ’ ’; InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare compare) { while(first1!= last1 && first2!= last2) if(compare(*first1,*first2)) ++first1 ; else if(compare(*first2,*first1)) ++first2; else { *result++ = *first1++; ++first2; } return result; }

enib c jt ...... 273/344 enib c jt ...... 274/344 & % & %

Ensembles Ensembles ' $ ' $

Exemple d’utilisation : set intersection (2) set difference (1) STL algo.h

char* word1 = "ABCDEFGHIJKLMNO"; template ostream iterator iter(cout, " "); OutputIterator set difference(InputIterator1 first1, InputIterator1 last1, cout << "word1 : "; InputIterator2 first2, InputIterator2 last2, copy(word1, word1 + : :strlen(word1), iter); OutputIterator result) { cout << "\nword2 : "; while(first1!= last1 && first2!= last2) copy(word2, word2 + : :strlen(word2), iter); if(*first1 < *first2) *result++ = *first1++; else if(*first2 < *first1) ++first2; set intersection(word1, word1 + : :strlen(word1), word2, else (++first1, ++first2); word2 + : :strlen(word2), iter, less()); return copy(first1, last1, result); cout << endl; }

enib c jt ...... 275/344 enib c jt ...... 276/344 & % & % Ensembles Ensembles ' $ ' $

Exemple d’utilisation : set difference (1) set difference (2) STL algo.h

template OutputIterator set difference(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare compare) { while(first1!= last1 && first2!= last2) if(compare(*first1,*first2)) *result++ = *first1++; else if(compare(*first2,*first1)) ++first2; else (++first1, ++first2); return copy(first1, last1, result); }

enib c jt ...... 277/344 enib c jt ...... 278/344 & % & %

Ensembles Recherches ' $ ' $

Exemple d’utilisation : set difference (2) Recherche d’un ´el´ement

int array[] = { 1, 3, 5, 6, 8, 12, 14, 15, 17, 20 } ; int* first = &array[0]; int* last = &array[10]; int value = (argc > 1)? atoi(argv[1]) : 14;

while(first!= last && *first!= value) ++first;

if(first!= last) cout << *first << endl;

enib c jt ...... 279/344 enib c jt ...... 280/344 & % & % Recherches Recherches ' $ ' $

find STL algo.h Exemple d’utilisation : find

template InputIterator find(InputIterator first, InputIterator last, const T& value) { while(first!= last && *first!= value) ++first; return first; }

enib c jt ...... 281/344 enib c jt ...... 282/344 & % & %

Recherches Recherches ' $ ' $

Recherche g´en´eralis´ee d’un ´el´ement find if STL algo.h

/* int even(int n) { return!(n % 2); } */ template InputIterator int array[] = { 1, 3, 5, 6, 8, 12, 14, 15, 17, 20 } ; find if(InputIterator first, InputIterator last, int* first = &array[0]; Predicate predicate) { int* last = &array[10]; while(first!= last &&!predicate(*first)) ++first; int (*predicate)(int) = even; return first; } while(first!= last &&!predicate(*first)) ++first;

if(first!= last) cout << *first << endl;

enib c jt ...... 283/344 enib c jt ...... 284/344 & % & % Recherches Recherches ' $ ' $

Exemple d’utilisation : find if Recherche de 2 ´el´ements adjacents identiques

int array[] = { 1, 3, 5, 6, 8, 12, 12, 15, 17, 20 } ; int* first = &array[3]; int* last = &array[8]; int* next = first;

while(++next!= last) { if(*first == *next) break; first = next; }

if(next!= last) cout << *first << " == " << *next << endl;

enib c jt ...... 285/344 enib c jt ...... 286/344 & % & %

Recherches Recherches ' $ ' $

adjacent find (1) STL algo.h Exemple d’utilisation : adjacent find (1)

template InputIterator adjacent find(InputIterator first, InputIterator last) { if(first == last) return last; InputIterator next = first; while(++next!= last) { if(*first == *next) return first; first = next; } return last; }

enib c jt ...... 287/344 enib c jt ...... 288/344 & % & % Recherches Recherches ' $ ' $

Recherche g´en´eralis´ee de 2 ´el´ements adjacents adjacent find (2) STL algo.h

/* int equal(int x, int y) return x == y; */ template InputIterator int array[] = { 1, 3, 5, 6, 8, 12, 12, 15, 17, 20 } ; adjacent find(InputIterator first, InputIterator last, int* first = &array[3]; BinaryPredicate predicate) { int* last = &array[8]; if(first == last) return last; int* next = first; InputIterator next = first; int (*predicate)(int,int) = equal; while(++next!= last) { if(predicate(*first,*next)) return first; while(++next!= last) { first = next; if(predicate(*first,*next)) break ; } first = next; return last; } }

enib c jt ...... 289/344 enib c jt ...... 290/344 & % & %

Recherches Recherches ' $ ' $

Exemple d’utilisation : adjacent find (2) Recherche d’une s´equence

int array1[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; int array2[] = { 6, 8, 12, 12, 15, 17, 6, 1, 3, 6 } ; int* first1 = &array1[2]; int* last1 = &array1[10]; int* first2 = &array2[3]; int* last2 = &array2[7];

ptrdiff t d1 = last1 - first1; ptrdiff t d2 = last2 - first2; int* result = last1;

enib c jt ...... 291/344 enib c jt ...... 292/344 & % & % Recherches Recherches ' $ ' $

Recherche d’une s´equence (suite) search (1) STL algo.h

if(d1 >= d2) { template int* current1 = first1; ForwardIterator1 int* current2 = first2; search(ForwardIterator1 first1, ForwardIterator1 last1, while(current2!= last2) { ForwardIterator2 first2, ForwardIterator2 last2) { if(*current1++!= *current2++) { ptrdiff t d1 = last1 - first1; if(d1-- == d2) result = last1; ptrdiff t d2 = last2 - first2; else { current1 = ++first1; current2 = first2; } ForwardIterator1 result = last1; } } `asuivre ... result = (current2 == last2)? first1 : last1; }

enib c jt ...... 293/344 enib c jt ...... 294/344 & % & %

Recherches Recherches ' $ ' $

search (1 - suite) STL algo.h Exemple d’utilisation : search (1)

if(d1 >= d2) { ForwardIterator1 current1 = first1; ForwardIterator2 current2 = first2; while(current2!= last2) { if(*current1++!= *current2++) { if(d1-- == d2) result = last1; else { current1 = ++first1; current2 = first2; } } } result = (current2 == last2)? first1 : last1; } return result; } ... fin enib c jt ...... 295/344 enib c jt ...... 296/344 & % & % Recherches Recherches ' $ ' $ Recherche g´en´eralis´ee d’une s´equence Recherche g´en´eralis´ee d’une s´equence (suite)

/* int equal(int x, int y) { return x == y; } */ if(d1 >= d2) { int* current1 = first1; int array1[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; int* current2 = first2; int array2[] = { 6, 8, 12, 12, 15, 17, 6, 1, 3, 6 } ; while(current2!= last2) { int* first1 = &array1[2]; if( !predicate(*current1++,*current2++)) { int* last1 = &array1[10]; if(d1-- == d2) result = last1; int* first2 = &array2[3]; else { current1 = ++first1; current2 = first2; } int* last2 = &array2[7]; } ptrdiff t d1 = last1 - first1; } ptrdiff t d2 = last2 - first2; result = (current2 == last2)? first1 : last1; int* result = last1; }

int (*predicate)(int,int) = equal; enib c jt ...... 297/344 enib c jt ...... 298/344 & % & %

Recherches Recherches ' $ ' $

search (2) STL algo.h search (2 -suite) STL algo.h

template = d2) { class BinaryPredicate> ForwardIterator1 current1 = first1; ForwardIterator1 ForwardIterator2 current2 = first2; search(ForwardIterator1 first1, ForwardIterator1 last1, while(current2!= last2) { ForwardIterator2 first2, ForwardIterator2 last2, if( !predicate(*current1++,*current2++)) { BinaryPredicate predicate) { if(d1-- == d2) result = last1; ptrdiff t d1 = last1 - first1; else { current1 = ++first1; current2 = first2; } ptrdiff t d2 = last2 - first2; } ForwardIterator1 result = last1; } result = (current2 == last2)? first1 : last1; `asuivre ... } return result; } ... fin enib c jt ...... 299/344 enib c jt ...... 300/344 & % & % Recherches Recherches ' $ ' $

Exemple d’utilisation : search (2) Occurences d’un ´el´ement

int array[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; int* first = &array[0]; int* last = &array[10]; int n0 = 2, n = n0; int value = (argc > 1)? atoi(argv[1]) : 6;

while(first!= last) if(*first++ == value) ++n;

if(first == last) cout << (n - n0) << endl;

enib c jt ...... 301/344 enib c jt ...... 302/344 & % & %

Recherches Recherches ' $ ' $

count STL algo.h Exemple d’utilisation : count

template void count(InputIterator first, InputIterator last, const T& value, Size& n) { while(first!= last) { if(*first++ == value) ++n; } }

enib c jt ...... 303/344 enib c jt ...... 304/344 & % & % Recherches Recherches ' $ ' $

Occurences conditionnelles count if STL algo.h

/* int even(int n) return!(n % 2); */ template void int array[] = { 1, 3, 6, 6, 8, 12, 12, 15, 17, 6 } ; count if(InputIterator first, InputIterator last, int* first = &array[0]; Predicate predicate, Size& n) { int* last = &array[10]; while(first!= last) { int n0 = 2, n = n0; if(predicate(*first++)) ++n ; int (*predicate)(int) = even; } } while(first!= last) if(predicate(*first++)) ++n;

if(first == last) cout << (n - n0) << endl;

enib c jt ...... 305/344 enib c jt ...... 306/344 & % & %

Recherches Recherches ' $ ' $

Exemple d’utilisation : count if Recherche dichotomique

enib c jt ...... 307/344 enib c jt ...... 308/344 & % & % Recherches Recherches ' $ ' $

binary search (1) STL algo.h binary search (1 - suite) STL algo.h

template while(length > 0) { bool half = length / 2; middle = first + half; binary search(RandomAccessIterator first, if(*middle < value) { RandomAccessIterator last, const T& value) { first = middle + 1; ptrdiff t length = last - first; length = length - half - 1; ptrdiff t half = length / 2; } RandomAccessIterator middle; else length = half; } `asuivre ... return first!= last &&!(value < *first); } ... fin

enib c jt ...... 309/344 enib c jt ...... 310/344 & % & %

Recherches Recherches ' $ ' $

Exemple d’utilisation : binary search (1) Recherche dichotomique g´en´eralis´ee

enib c jt ...... 311/344 enib c jt ...... 312/344 & % & % Recherches Recherches ' $ ' $

binary search (2) STL algo.h binary search (2 - suite) STL algo.h

template 0) { class Compare> half = length / 2; middle = first + half; bool if(compare(*middle,value)) { binary search(RandomAccessIterator first, first = middle + 1; RandomAccessIterator last, length = length - half - 1; const T& value, Compare compare) { } ptrdiff t length = last - first; else length = half; ptrdiff t half = length / 2; } RandomAccessIterator middle; return first!= last &&!compare(value,*first); } `asuivre ...... fin

enib c jt ...... 313/344 enib c jt ...... 314/344 & % & %

Recherches Standard Template Library ' $ ' $

Exemple d’utilisation : binary search (2) Index

Index 1. Liste des fichiers STL ...... 317 2. Index th´ematique des classes ...... 318 3. Index th´ematique des fonctions ...... 323 4. Index alphab´etique des classes ...... 332 5. Index alphab´etique des fonctions ...... 336

enib c jt ...... 315/344 enib c jt ...... 316/344 & % & % Standard Template Library Standard Template Library ' $ ' $

Liste des fichiers STL Index th´ematique des classes

Index th´ematique des classes algo.h multimap.h algobase.h multiset.h 1.It´erateurs ...... 319 bool.h pair.h bvector.h projectn.h 2.Conteneurs ...... 320 defalloc.h set.h 3.Fonctions ...... 321 deque.h stack.h function.h tempbuf.h iterator.h tree.h map.h vector.h

enib c jt ...... 317/344 enib c jt ...... 318/344 & % & %

Standard Template Library Standard Template Library ' $ ' $ Index th´ematique des classes Index th´ematique des classes

It´erateurs Conteneurs back insert iterator ...... 36 deque ...... 74—79 bidirectional iterator ...... 17,29 list ...... 81—88 forward iterator ...... 17,25 map ...... 116 — 124 front insert iterator ...... 36 multimap ...... 125 — 133 input iterator ...... 17, 21 multiset ...... 108 — 115 insert iterator ...... 36 pair ...... 97—97 istream iterator ...... 36 priority queue ...... 142—144 ostream iterator ...... 36 queue ...... 139 — 141 output iterator ...... 17, 18 rb tree ...... 90—96 random access iterator ...... 17,34 set ...... 100 — 107 reverse bidirectional iterator ...... 37 stack ...... 136 — 138 reverse iterator ...... 37 vector ...... 67—72 enib c jt ...... 319/344 enib c jt ...... 320/344 & % & % Standard Template Library Standard Template Library ' $ ' $ Index th´ematique des classes Index th´ematique des classes

Classes de fonctions Classes de fonctions binary compose ...... 63 minus ...... 45 binary function ...... 44 modulus ...... 47 divides ...... 46 negate ...... 47 equal to ...... 49 not equal to ...... 49 greater ...... 50 plus ...... 45 greater equal ...... 50 pointer to unary function ...... 56 ident ...... 52 pointer to binary function ...... 58 less ...... 51 select1st ...... 52 less equal ...... 51 times ...... 46 logical and ...... 53 unary compose ...... 60 logical or ...... 53 unary function ...... 44 logical not ...... 54 enib c jt ...... 321/344 enib c jt ...... 322/344 & % & %

Standard Template Library Standard Template Library ' $ ' $ Index th´ematique des fonctions Index th´ematique des fonctions

Index th´ematique des fonctions Calculs 1.Calculs ...... 324 accumulate (1) ...... 147 accumulate (2) ...... 150 2.Comparaisons ...... 325 adjacent difference (1) ...... 165 3.Manipulations ...... 327 adjacent difference (2) ...... 168 inner product (1) ...... 153 4.Allocations ...... 329 inner product (2) ...... 156 5.Ensembles ...... 330 partial sum (1) ...... 159 partial sum (2) ...... 162 6.Recherches ...... 331

enib c jt ...... 323/344 enib c jt ...... 324/344 & % & % Standard Template Library Standard Template Library ' $ ' $ Index th´ematique des fonctions Index th´ematique des fonctions

Comparaisons Comparaisons (suite) equal (1) ...... 195 min (2) ...... 181 equal (2) ...... 198 min element (1) ...... 184 lexicographical compare (1) ...... 201 min element (2) ...... 186 lexicographical compare (2) ...... 204 mismatch (1) ...... 189 max (1) ...... 173 mismatch (2) ...... 192 max (2) ...... 173 operator> ...... 170 max element (1) ...... 176 operator<= ...... 170 max element (2) ...... 178 operator>= ...... 170 min (1) ...... 181 operator != ...... 170

enib c jt ...... 325/344 enib c jt ...... 326/344 & % & %

Standard Template Library Standard Template Library ' $ ' $ Index th´ematique des fonctions Index th´ematique des fonctions

Manipulations Manipulations (suite) for each ...... 214 replace ...... 225 generate ...... 216 replace copy ...... 229 generate n ...... 219 replace copy if ...... 231 iter swap ...... 209 replace if ...... 227 partition ...... 241 swap ...... 207 remove ...... 237 swap ranges ...... 211 remove copy ...... 233 transform (1) ...... 221 remove copy if ...... 235 transform (2) ...... 223 remove if ...... 239

enib c jt ...... 327/344 enib c jt ...... 328/344 & % & % Standard Template Library Standard Template Library ' $ ' $ Index th´ematique des fonctions Index th´ematique des fonctions

Allocations Ensembles

enib c jt ...... 329/344 enib c jt ...... 330/344 & % & %

Standard Template Library Standard Template Library ' $ ' $ Index th´ematique des fonctions Index alphab´etique des classes

back insert iterator ...... 36 Recherches bidirectional iterator ...... 17,29 binary compose ...... 63 binary function ...... 44 deque ...... 74 — 79 divides ...... 46 equal to ...... 49 forward iterator ...... 17,25 front insert iterator ...... 36 greater ...... 50 greater equal ...... 50

enib c jt ...... 331/344 enib c jt ...... 332/344 & % & % Standard Template Library Standard Template Library ' $ ' $ Index alphab´etique des classes Index alphab´etique des classes

ident ...... 52 modulus ...... 47 input iterator ...... 17,21 multimap ...... 125—133 insert iterator ...... 36 multiset ...... 108—115 istream iterator ...... 36 negate ...... 47 less ...... 51 not equal to ...... 49 less equal ...... 51 ostream iterator ...... 36 list ...... 81—88 output iterator ...... 17,18 logical and ...... 53 pair ...... 97—97 logical or ...... 53 plus ...... 45 logical not ...... 54 pointer to unary function ...... 56 map ...... 116 — 124 pointer to binary function ...... 58 minus ...... 45 priority queue ...... 142—144

enib c jt ...... 333/344 enib c jt ...... 334/344 & % & %

Standard Template Library Standard Template Library ' $ ' $ Index alphab´etique des classes Index alphab´etique des fonctions

queue ...... 139 — 141 accumulate (1) ...... 147 random access iterator ...... 17,34 accumulate (2) ...... 150 rb tree ...... 90 — 96 adjacent difference (1) ...... 165 reverse bidirectional iterator ...... 37 adjacent difference (2) ...... 168 reverse iterator ...... 37 adjacent find (1) ...... 287 select1st ...... 52 adjacent find (2) ...... 290 set ...... 100 — 107 binary search (1) ...... 309 stack ...... 136 — 138 binary search (2) ...... 313 times ...... 46 construct ...... 244 unary compose ...... 60 copy ...... 258 unary function ...... 44 copy backward ...... 260 vector ...... 67—72 count ...... 303

enib c jt ...... 335/344 enib c jt ...... 336/344 & % & % Standard Template Library Standard Template Library ' $ ' $ Index alphab´etique des fonctions Index alphab´etique des fonctions

count if ...... 306 includes (1) ...... 264 destroy (1) ...... 246 includes (2) ...... 266 destroy (2) ...... 248 inner product (1) ...... 153 equal (1) ...... 195 inner product (2) ...... 156 equal (2) ...... 198 iter swap ...... 209 fill ...... 250 lexicographical compare (1) ...... 201 fill n ...... 252 lexicographical compare (2) ...... 204 find ...... 281 max (1) ...... 173 find if ...... 284 max (2) ...... 173 for each ...... 214 max element (1) ...... 176 generate ...... 216 max element (2) ...... 178 generate n ...... 219 min (1) ...... 181

enib c jt ...... 337/344 enib c jt ...... 338/344 & % & %

Standard Template Library Standard Template Library ' $ ' $ Index alphab´etique des fonctions Index alphab´etique des fonctions

min (2) ...... 181 remove copy ...... 233 min element (1) ...... 184 remove copy if ...... 235 min element (2) ...... 186 remove if ...... 239 mismatch (1) ...... 189 search (1) ...... 294 mismatch (2) ...... 192 search (2) ...... 299 operator> ...... 170 set difference (1) ...... 276 operator<= ...... 170 set difference (2) ...... 278 operator>= ...... 170 set intersection (1) ...... 272 operator != ...... 170 set intersection (2) ...... 274 partial sum (1) ...... 159 set union (1) ...... 268 partial sum (2) ...... 162 set union (2) ...... 270 remove ...... 237 swap ...... 207

enib c jt ...... 339/344 enib c jt ...... 340/344 & % & % Standard Template Library Standard Template Library ' $ ' $

Index alphab´etique des fonctions R´ef´erences

swap ranges ...... 211 R´ef´erences transform (1) ...... 221 1. Bibliographie ...... 343 transform (2) ...... 223 2. Sites Internet ...... 344 uninitialized copy ...... 262 uninitialized fill ...... 254 uninitialized fill n ...... 256

enib c jt ...... 341/344 enib c jt ...... 342/344 & % & %

R´ef´erences STL R´ef´erences STL ' $ ' $

Bibliographie Sites Internet

Musser D.R., Saini A., STL tutorial and reference guide : ftp ://research.att.com/dist/c++std/WP/ C++ programming with the Standard Template Library, Addison–Wesley — 1996 http ://weber.u.washington.edu/~bytewave/bytewave stl.html

Stepanov A., Lee M., The Standard Template Library, http ://www.ualberta.ca/~nyu/stl/stl.html Hewlett–Packard — 1995 http ://www.aw.com/cp/musser-saini.html

enib c jt ...... 343/344 enib c jt ...... 344/344 & % & %