P.A._IG Cha- 128
M. BENJELLOUN : 2020-21 UMONS
S6
Conteneur Séquence
…vector list
vector <int> v(10);
for (int i=0; i<v.size(); i++) v[i] = i;
vector<int> :: iterator iV ;
// itérateur directfor (iV=V.begin(); iV !=V.end(); iV++) cout<<*iV<<" ; " ;
#include <vector> #include <list>
list <int> L;
list<int> :: iterator iV ; for (iV=L.begin(); iV !=L.end(); iV++)
cout<<*iV<<" ; " ;
P.A._IG Cha- 130
M. BENJELLOUN : 2020-21 UMONS
Conteneur Séquence
…vector list
vector <int> v(10);
for (int i=0; i<v.size(); i++) v[i] = i;
vector<int> :: iterator iV ;
// itérateur directfor (iV=V.begin(); iV !=V.end(); iV++) cout<<*iV<<" ; " ;
#include <vector> #include <list>
list <int> L;
list<int> :: iterator iV ; for (iV=L.begin(); iV !=L.end(); iV++)
cout<<*iV<<" ; " ; template <class A> void Affiche(A Tab) {
typename A::iterator it;
for(it=Tab.begin();it!=Tab.end();it++)
cout << *it << " ";}
// it→affichage();P.A._IG Cha- 131
M. BENJELLOUN : 2020-21 UMONS
Conteneur Séquence
…vector list
template <class A> void Affiche(A Tab) { typename A::iterator it;
for(it=Tab.begin();it!=Tab.end();it++)
cout << *it << " ";}
// it→affichage();void saisie(vector<PROF> &P, int &N){
cout<<" N ??"<<endl;
cin>>N;
for(int i=0;i<N;i++){
P[i].Saisie();
} }
vector<PROF> Profs; // dans main() vector<PROF> Profs(10);
Et OK si
P.A._IG Cha- 132
M. BENJELLOUN : 2020-21 UMONS
Problème de saisie (ayant comme résultat le même id pour tous les éléments de la liste ou du vector)
… void SAISIE(A &tab) { B B1;
char chx;
do {
B1.saisie();
tab.insert(tab.end(),B1);
AFFICHAGE(tab);
cout<<"Vous voulez continuer à saisir?oui(O)/non(N)"<<endl;
cin>>chx;
}while((chx=='o')||(chx==‘O'));
}
Le constructeur est censé gérer les Id's
Conteneur Séquence
…vector list
Conteneur Séquence
…vector list
vector <int> v;
v.push_back(i);
v.pop_back();
v.insert(i);
v.erase(iV);
#include <vector>
L.push_back(i) L.push_front(i);
L.pop_back();
L.pop_front();
L.insert(itL,3,5);
L.erase(itL) list<int> L;
vector<int> :: iterator iV ;
v.insert(itL,3,5);
#include <list>
P.A._IG Cha- 134
M. BENJELLOUN : 2020-21 UMONS
Conteneur Séquence
…vector list
v.erase(itV);
#include <vector>
L.erase(itL)
#include <list>
it = find(LV.begin(), LV.end(), ELEM);
LV.erase(it);
Attention à la position de l’iterator !!
L.empty() v.empty()
L.size() v.size()
P.A._IG Cha- 135
M. BENJELLOUN : 2020-21 UMONS
Conteneur Séquence
…vector list #include <vector> #include <list>
L.unique();
L.remove(‘B’);
L.sort( );
V.unique();
V.remove(‘B’);
V.sort( );
P.A._IG Cha- 136
M. BENJELLOUN : 2020-21 UMONS
for_each find find_if find_first_of count
count_if equal search search_n find_end
copy copy_n swap replace fill fill_n generate remove unique reverse rotate
sort partial_sort nth_element lower_bound upper_bound merge min max
min_element max_element
lexicographical_compare
STL
sort ( V.begin(), V.end());
sort ( V.begin(), V.end(), cmp );
it = find (L.begin(), L.end(), “Texte");
it = find (V.begin(), V.end(), “Texte");
#include <algorithm>
Algorithmes
count(V.begin(), V.end(), 'b'); replace(V.begin(), V.end(), 'd', 'f’);
template <class A>
void trouver(A V) { ...
itc= find(V.begin(), V.end(), temp); // operator==
if (itc != V.end()) {
cout << "L'élément recherché EXISTE" << endl;
itc->afficher();
} else {
cout << "l'élément ne se trouve pas dans le tableau" << endl;
} ...
TP5
P.A._IG Cha- 138
M. BENJELLOUN : 2020-21 UMONS Personne
int Id ; string nom;
public : void saisie();
void afficher();
Etudiant string An_Scol ; int Note[3];
public : void saisie();
void afficher() ;
Prof string Service;
public : void saisie();
void afficher();
Direction string Titre;
public : void saisie();
void afficher();
..
voidPersonne:: saisie() {
cout<<" \nEntrez son nom : ";
cin.ignore(); // Pour ignorer un éventuel "Enter"
getline(cin,Nom);
//cout<<" \nEntrez son Id : ";
//cin>>Id;
cout<<"\n"<<endl;
}
voidPROF:: saisie() { Personne:: saisie();
cout<<" Entrez le nom du Service : ";
cin>>Service;
cout<<" \n";
}
voidDirection:: saisie() { PROF:: saisie();
cout<<" Entrez le nom du Titre : ";
cin>>Titre;
cout<<" \n";
}
TP6
P.A._IG Cha- 139
M. BENJELLOUN : 2020-21 UMONS Personne
int Id ; string nom;
public : void saisie();
void afficher();
Etudiant string An_Scol ; int Note[3];
public : void saisie();
void afficher() ;
Prof string Service;
public : void saisie();
void afficher();
Direction string Titre;
public : void saisie();
void afficher();
..
classPersonne { intId;
string Nom;
public:
voidsaisie();
voidafficher();
string getNom();
intReturnId();
};
classEtudiant: publicPersonne { string anne_scol;
intNote[Note_Max];
public:
voidsaisie();
voidafficher();
string get_an() ; intgetNote(inti) {
returnNotes[i];
} };
classDirection;
friendDirection;
4 : Doyen membre de Direction Calcule la Moyenne de chaque étudiant 5 : Cherchersi 1 Element (Etud ou Prof) existe ;
booloperator < (PersonneP) { return nom< P.nom ; }
TP6
protected:P.A._IG Cha- 140
M. BENJELLOUN : 2020-21 UMONS Personne
int Id ; string nom;
public : void saisie();
void afficher();
Etudiant string An_Scol ; int Note[3];
public : void saisie();
void afficher() ;
Prof string Service;
public : void saisie();
void afficher();
Direction string Titre;
public : void saisie();
void afficher();
..
classDirection: publicPROF{
…
public:
...
floatmoyenne(EtudiantE) { floatmoyenne = 0;
for(inti = 0 ; i < Note_Max; i++) moyenne += E.Note[i];
...
...
} };4 : Doyen membre de Direction Calcule la Moyenne de chaque étudiant 5 : Cherchersi 1 Element (Etud ou Prof) existe ;
TP6
Personne int Id ; string nom;
public : void saisie();
void afficher();
Etudiant string An_Scol ; int Note[3];
public : void saisie();
void afficher() ;
Prof string Service;
public : void saisie();
void afficher();
Direction string Titre;
public : void saisie();
void afficher();
..
4 : Doyen membre de Direction Calcule la Moyenne de chaque étudiant 5 : Cherchersi 1 Element (Etud ou Prof) existe ;
classPersonne{ ...public:
booloperator ==(Personnep) {
if(nom ==p.nom) return true;
else return false;
...} };
…cout <<"Quel element recherchez-vous ?"<<endl;
temp.saisie();
it = find(tab.begin(), tab.end(), temp);
…
TP6
P.A._IG Cha- 142
M. BENJELLOUN : 2020-21 UMONS Personne
int Id ; string nom;
public : void saisie();
void afficher();
Etudiant string An_Scol ; int Note[3];
public : void saisie();
void afficher() ;
Prof string Service;
public : void saisie();
void afficher();
Direction string Titre;
public : void saisie();
void afficher();
..
TP6
void Lecture( T &V, string f,Ak) { int n;
ifstream lecture;
lecture.open(f);
if(lecture) { lecture >> n;
lecture.ignore();
. . .
P.A._IG Cha- 143
M. BENJELLOUN : 2020-21 UMONS
resize copy
includes
accumulate
count
P.A._IG Cha- 144
M. BENJELLOUN : 2020-21 UMONS
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
void main() { list<int> liste;
vector<int> vecteur;
// insérer les valeurs 1 à 10 dans la liste
// Adapter la taille du vecteur à la taille de la liste
//Verser le contenu de la liste dans le vecteur }
for (int i=1; i<10; i++) { liste.push_back(i);
}
vecteur.resize ( liste.size() );
copy (liste.begin(), liste.end(),
// sourcevecteur.begin());
// destinationresize copy
… struct E {
int X;
string nom;
};
void main() { list<E> liste;
vector<E> vecteur;
// insérer les valeurs 0 à 5 dans la liste
// Adapter la taille du vecteur à la taille de la liste
//Verser le contenu de la liste dans le vecteur
}
for (int i=0; i<5; i++) {
E Etud;Etud.X = i;
Etud.nom = " N" + to_string(i) ; liste.push_back(Etud);
}
vecteur.resize ( liste.size() );
copy (liste.begin(), liste.end(),
// sourcevecteur.begin());
// destinationresize
copy
P.A._IG Cha- 146
M. BENJELLOUN : 2020-21 UMONS string str1 = "12";
stringstr2 = "3.45";
stringstr3 = "67 Et 8 ";
//string str4 = "Ceci est un 9";
int int1 = stoi(str1);
int int2 = stoi(str2);
int int3 = stoi(str3);
// int int4 = stoi(str4); // error: 'invalid_argument'
cout <<"stoi(\"" << str1 <<"\") EST " << int1 <<'\n';
cout <<"stoi(\"" << str2 <<"\") EST " << int2 <<'\n';
cout <<"stoi(\"" << str3 <<"\") EST " << int3 <<"\n\n";
floatfloat1 = stof(str1);
floatfloat2 = stof(str2);
cout <<"stof(\"" << str1 <<"\") EST " << float1 << '\n';
cout <<"stof(\"" << str2 <<"\") EST " << float2 << '\n';
stoi et stof
P.A._IG Cha- 147
M. BENJELLOUN : 2020-21 UMONS
STL
#include<iostream>
#include<vector>
#include<list>
#include <algorithm>
using namespacestd;
voidmain() {
int tab[5] = {3, 2, 9, 1, 4}, T[2] = { 9, 1} ; list<int> L, L2;
vector <int> v(5), v2(2);
for ( int i = 0; i < 5; i++ ) {
v.insert(v.end(), tab[i]); L.insert(L.end(), tab[i]); } v2.insert(v2.end(),9); v2.insert(v2.end(),1);
L2.insert(L2.end(),9); L2.insert(L2.end(),1);
if ( includes(tab, tab+5, T, T+2) ) cout << "tab contient T" << endl;
else cout << "tab NE contient PAS T" << endl;
if ( includes(L.begin(), L.end(), L2.begin(), L2.end()) ) cout << "L contient L2" << endl;
else cout << "L NE contient PAS L2" << endl;
if ( includes(v.begin(), v.end(), v2.begin(), v2.end()) ) cout << "v contient v2" << endl;
else cout <<"v NE contient PAS v2" << endl;
system("pause");
}
Algorithme de détermination d'inclusion
P.A._IG Cha- 148
M. BENJELLOUN : 2020-21 UMONS
#include<iostream>
#include<numeric> // pour accumulate
#include<vector>
#include<list>
using namespacestd;
void
main() {
int
tab[5] = {3, 2, 9, 1, 4};
list<int> L;
vector <int> v(5);
for(inti = 0; i < v.size(); i++) { v[i]= tab[i]; L.insert(L.end(), tab[i]); }
// Calculer la somme des éléments avec 0 comme valeur initiale
cout << "som Tab= "<< accumulate(tab, tab + 5, 0) << endl; // 19 = 3+2+9+1+4+0 cout << "som V = "<< accumulate(v.begin(),v.end(), 0) << endl; // 19
cout << "som L = " << accumulate(L.begin(),L.end(), 0) << endl; // 19
cout << "mult Tab= "<< accumulate(tab, tab + 5, 1, multiplies<int>()) << endl; // 216 cout << "mult V = " << accumulate(v.begin(),v.end(), 1, multiplies<int>()) << endl; // 216 cout << "mult L = " << accumulate(L.begin(),L.end(), 1, multiplies<int>()) << endl; // 216 cout << "mult Tab= "<< accumulate(tab, tab + 5, 10, multiplies<int>()) << endl; // 2160 cout << "mult V = "<< accumulate(v.begin(),v.end(), 10, multiplies<int>()) << endl; // 2160 cout << "mult L = "<< accumulate(L.begin(),L.end(), 10, multiplies<int>()) << endl; // 2160
system("pause");
}
accumulate
Exemple d’algorithme numérique (accumule)
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
using namespacestd;
void main() {
int
t[10] = {0, 1, 1, 3, 4, 4, 4, 9, 8, 9};
list<int> L;
vector<int> V;
for
(int i=0; i<10; ++i) { L.push_back(t[i]);
V.push_back(t[i]);
}
for (int i=0; i<10; ++i)
cout << "V : pour " << i << " y a " << count(V.begin(), V.end(), i) << endl;
for (int i=0; i<10; ++i)
cout << "L : pour " << i << " y a " << count(L.begin(), L.end(), i) << endl;
}
Nombre de i ?
count
P.A._IG Cha- 150
M. BENJELLOUN : 2020-21 UMONS
void
main() {
vector<int> T;intvaleur;
...
// T = 0 1 2
for(int i=0; i<4; i++) {
cout << " Ajouter votre nouvelle valeur :" ; cin >> valeur;
if
( count(T.begin(), T.end(), valeur) )
cout << " Elle existe déjà !! " << endl;
else
T.push_back(valeur); Affiche(T);
}
cout << "Quelle valeur désirez-vous enlever ?"; cin >> valeur;
T.erase( find (T.begin(), T.end(), valeur) );
Affiche(T);
}
0 1 2
Ajouter votre nouvelle valeur : 8 0 1 2 8
Ajouter votre nouvelle valeur : 2 Elle existe déjà !!
Ajouter votre nouvelle valeur : 9 0 1 2 8 9
Ajouter votre nouvelle valeur : 1 Elle existe déjà !!
Quelle valeur désirez-vous enlever ? 0 1 2 8 9
#include<vector>
#include<iostream>
#include<algorithm>
using namespacestd;
voidAffiche ( vector<int> tab) {
for(inti=0; i<tab.size(); i++) cout << tab[i] << ' ';
cout << endl;
}
Nbre de x valeur
P.A._IG Cha- 151
M. BENJELLOUN : 2020-21 UMONS
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void affiche(vector<int> V, string S){
cout << S << " : ";
for (int i=0; i< V.size() ; i++) { cout <<V[i] <<" ";
}
cout << endl;
}
void main() {
int T[7] = { 3, 5, 4, 9, 1, 9, 1};
int Seq[2] = {9, 1};
int *p ;
vector<int> V(T, T+7); // itérateur affiche(V, "V Initial");
p= search(T, T+7, Seq, Seq+2);
cout << "... En V :{9,1} en position " << p - T << endl;
cout << "Min Ts: " << *min_element(V.begin(),V.end()) << endl;
cout << "Min It: " << *min_element(T, T+7) << endl;
cout << "Max It: " << *max_element(T, T+7) << endl;
cout << "Max Ts : " << *max_element(V.begin(),V.end()) << endl;
cout << "Max It+2: " << *max_element(T, T+2) << endl;
sort(V.begin(), V.end()); affiche(V, "V Trie ");
sort(T, T+7);
cout << "Pour V Trie: 5 peut etre insere de "
<< lower_bound(T, T+7, 5) - T << " a "
<< upper_bound(T, T+7, 5) - T << endl;
replace(V.begin(), V.end(), 5, 4);
affiche(V, "V Trie Replace 5 par 4");
}
V Initial : 3 5 4 9 1 9 1... En V :{9,1} en position 3 Min Ts: 1
Min It: 1 Max It: 9 Max Ts : 9 Max It+2: 5
V Trie : 1 1 3 4 5 9 9
Pour V Trie: 5 peut etre insere de 4 a 5 V Trie Replace 5 par 4 : 1 1 3 4 4 9 9
STL
P.A._IG Cha- 152
M. BENJELLOUN : 2020-21 UMONS
7 Etud1 Etud2 Etud3 Etud4 Etud5 Etud6 Etud7
Etudiant.txt
5 ZProf1; Informatique Prof2; Informatique AProf3; Mathematique Prof4; Physique Prof5; Chimie Prof.txt
void LECture(Etudiant tab[], int &N) { ifstream lecture;
lecture.open("Etudiant.txt");
if(lecture) { lecture >> N;
for (int i = 0; i < N; i++) { lecture >> tab[i].nom;
}
lecture.close();
} else {
cout << "Le fichier Etudiant n'existe pas."<< endl;
} }
void LECture(PROF tab[], int &N) { ifstream lecture;
lecture.open("Prof.txt");
if (lecture) { lecture >> N;
lecture.ignore(); // Absorber le '\n’
for (int i = 0; i < N; i++) { getline(lecture, tab[i].nom, ';’);
getline(lecture, tab[i].Service, '\n');
}
lecture.close();
} else {
cout << "Le fichier Prof n'existe pas."<< endl;} }
Fichiers
7 Etud1 Etud2 Etud3 Etud4 Etud5 Etud6 Etud7
Etudiant.txt
5 ZProf1; Informatique Prof2; Informatique AProf3; Mathematique Prof4; Physique Prof5; Chimie Prof.txt
void LECture(Etudiant tab[], int &N,string S){
ifstream lecture;
lecture.open(S);
if(lecture) { lecture >> N;
for (int i = 0; i < N; i++) { lecture >> tab[i].nom;
}
lecture.close();
} else {
cout << "Le fichier" <<S << "n'existe pas."<< endl;
} }
void LECture(PROF tab[], int &N) { ifstream lecture;
lecture.open("Prof.txt");
if (lecture) { lecture >> N;
lecture.ignore(); // Absorber le '\n’
for (int i = 0; i < N; i++) { getline(lecture, tab[i].nom, ';’);
getline(lecture, tab[i].Service, '\n');
}
lecture.close();
} else {
cout << "Le fichier Prof n'existe pas."<< endl;} }
Fichiers
P.A._IG Cha- 154
M. BENJELLOUN : 2020-21 UMONS
Etudiant.txt Prof.txt
void LECture(Etudiant tab[], int &N, stringS) { ifstream lecture;
lecture.open(S);
if(lecture) { lecture >> N;
for (int i = 0; i < N; i++) { lecture >> tab[i].nom;
}
lecture.close();
} else {
cout << "Le fichier" <<S << "n'existe pas."<< endl;
} }
void LECture(PROF tab[], int &N, string S) { ifstream lecture;
lecture.open(S);
if (lecture) { lecture >> N;
lecture.ignore(); // Absorber le '\n’
for (int i = 0; i < N; i++) { getline(lecture, tab[i].nom, ';’);
getline(lecture, tab[i].Service, '\n');
}
lecture.close();
} else {
cout <<"Le fichier " << S << " n'existe pas."<< endl;} }
void
Prof::Data(ifstream
&lecF) {getline(lecF, nom, ';’);
getline(lect, service, '\n');
}
voidPersonne::Data(ifstream
&lecF) {getline(lectF, nom);
}
Fichiers
P.A._IG Cha- 155
M. BENJELLOUN : 2020-21 UMONS
void LECture(Etudiant tab[], int &N, stringS) { ifstream lecture;
lecture.open(S);
if(lecture) { lecture >> N;
lecture.ignore(); // Absorber le ‘\n’
for (int i = 0; i < N; i++) {
tab[i].Data(lecture);
}
lecture.close();
} else {
cout << "Le fichier" <<S << "n'existe pas."<< endl;} }
void LECture(PROF tab[], int &N, string S) { ifstream lecture;
lecture.open(S);
if (lecture) { lecture >> N;
lecture.ignore(); // Absorber le '\n’
for (int i = 0; i < N; i++) {
tab[i].Data(lecture);
}
lecture.close();
} else {
cout <<"Le fichier " << S << " n'existe pas."<< endl;} }
Fichiers
Etudiant.txt Prof.txt
void
Prof::Data(ifstream
&lecF) {getline(lecF, nom, ';’);
getline(lect, service, '\n');
}
voidPersonne::Data(ifstream
&lecF) {getline(lectF, nom);
}
P.A._IG Cha- 156
M. BENJELLOUN : 2020-21 UMONS
template <class T,class A>
void Lecture( T &V, string S, A k) { int N;
ifstream lecture;
lecture.open(S);
if (lecture) { lecture >> N;
lecture.ignore();
for (int i = 0; i < N; i++) { // on peut mettre while(!=lecture.eof()) A temp;
temp.Lec(lecture); // équiv à Data(ifstream &lecF) V.push_back(temp);
} lecture.close();
} else {
cout <<"Le fichier " << S << " n'existe pas."<< endl; } }
Conteneur
iterator begin() iterator end()
bool empty
…
Séquence vector list deque
Associatif
map multimap set multiset
Paramétrés par la clé et la valeur Paramétrés par la clé
set <K,Cmp >
map <K,V,Cmp >
Le tableau associatif
P.A._IG Cha- 158
M. BENJELLOUN : 2020-21 UMONS
On trouve quatre conteneurs associatifs ( ensembles ordonnées )
• set <K
Clé, Cmp > ; map <K
Clé, V
valeur, Cmp >
Cmp = fonction qui définit la comparaison entre deux clés.
Croissant par défaut ( set <K > ; map <K, V > ).
Le tableau associatif
STL A partir de la clé K , on accède à la valeur associée V .
P.A._IG Cha- 159
M. BENJELLOUN : 2020-21 UMONS
Conteneur
iterator begin() iterator end()
bool empty
…
Séquence vector list deque
Associatif
map multimap set multiset
Paramétrés par la clé et la valeur Paramétrés par la clé
Le tableau associatif
A partir de la clé, on accède à la valeur associée Ils sont
particulièrement adaptés lorsqu'on a besoin de réaliser un
grand nombre d'opérations de recherche .
P.A._IG Cha- 160
M. BENJELLOUN : 2020-21 UMONS
Heros.insert("Tintin");
#include<iostream>
#include<string>
#include<set>
using namespacestd;
void
main() { set<string> Heros;
set<string>::iterator H;
set<int> Nbre;
set<int>::iterator it;
for
(H= Heros.begin(); H != Heros.end(); H++) cout<< *H << endl;
Nbre.insert(9); Nbre.insert(3); Nbre.insert(6);
for
(it= Nbre.begin(); it != Nbre.end(); it++) cout<<
*it<< endl;
}
Affichage :
Tintin Heros.insert("Asterix");
Asterix Heros.insert("Obelix");
Obelix Heros.insert("Asterix");
Heros.insert("Titeuf");
Titeuf Heros.insert("Obelix");
3 6 9
Le tableau associatif Set
#include<iostream>
#include<string>
#include<set>
using namespacestd;
void
main() {
multiset<string> Heros;
multiset<string>::iterator H;
set<int> Nbre;
set<int>::iterator it;
Heros.insert("Tintin");
Heros.insert("Asterix");
Heros.insert("Obelix");
Heros.insert("Asterix");
Heros.insert("Titeuf");
Heros.insert("Obelix");
for
(H= Heros.begin(); H != Heros.end(); H++) cout<< *H << endl;
Nbre.insert(9); Nbre.insert(3); Nbre.insert(6);
for
(it= Nbre.begin(); it != Nbre.end(); it++) cout<< *it << endl;
}
Le tableau associatif Multi Set
Affichage :
Asterix
Asterix
Obelix
Obelix
Tintin
Titeuf
3 6
9
P.A._IG Cha- 162
M. BENJELLOUN : 2020-21 UMONS
#include<iostream>
#include<string>
#include<set>
using namespacestd;
voidmain() {
multiset<string> Heros;
multiset<string>::iterator H;
setlocale(LC_ALL,"");
Heros.insert("Tintin");
Heros.insert("Asterix");
Heros.insert("Obelix");
Heros.insert("Asterix");
Heros.insert("Titeuf");
Heros.insert("Obelix");
for(H= Heros.begin(); H != Heros.end(); H++) cout<< *H<< endl;
if(Heros.find("Obelix") != Heros.end()) cout << "Obelix est là"<< endl;
cout << "Nombre de ‘Asterix’ : "<< Heros.count("Asterix") << endl;
cout << "Nombre de ‘Titeuf’ : "<< Heros.count("Titeuf") << endl;
Heros.clear();
if(Heros.empty()) cout << "L’ensemble est vidé"<< endl;
else cout << "MultiMap n'est pas vide"<< endl;
}
Le tableau associatif Multi Set
Affichage : Asterix Asterix Obelix Obelix Tintin Titeuf Obelix est là
Nombre de 'Asterix' : 2 Nombre de 'Titeuf' : 1 L'ensemble est vidé
P.A._IG Cha- 163
M. BENJELLOUN : 2020-21 UMONS
Dans une map, les objets stockés sont des ‘pair’. Pour chaque paire, l'attribut first correspond à la clé Kalors que second est la valeur V.
Le tableau associatif Map
#include
<iostream>
#include
<string>
#include<map>
using namespace
std;
void main() {
map<string, float> poids;
// table qui associe le nom d'un animal à son poidsmap<string, float>::iterator it;
//On ajoute les poids de quelques animaux // poids[K] = V ; poids["souris"] = 0.05;
poids["tigre"] = 200;
poids["chat"] = 3;
poids["elephant"] = 10000;
for(it=poids.begin(); it!=poids.end(); it++) {
cout << it->first << " pese " << it->second << " kg." << endl;
}
system("pause");
}
map < K, V >
chat pese 3 kg.
elephant pese 10000 kg.
souris pese 0.05 kg.
tigre pese 200 kg.
P.A._IG Cha- 164
M. BENJELLOUN : 2020-21 UMONS
Le tableau associatif Map
map<char, int> map1, map2;
map < K, V >
map1['b’] = 300;
map1.insert ( pair<char,int>('a',100) );
map1.insert ( pair<char,int>('z',200) );
map1.insert ( make_pair('c',500) );
map1.insert ( make_pair('W',444) );
map1 ==>:
W => 444 a => 100 b => 300 c => 500 z => 200
map2.insert(map1.begin(),map1.find('c')); map2 ==>:
W => 444 a => 100 b => 300
Le tableau associatif Map
map<char, int> map1, map2;
map<char, int> :: iterator M;
map < K, V >
map1['b’] = 300;
map1.insert ( pair<char,int>('a',100) );
map1.insert ( pair<char,int>('z',200) );
map1.insert ( make_pair('c',500) );
map1.insert ( make_pair('W',444) );
map1 ==>:
W => 444 a => 100 b => 300 c => 500 z => 200 map2 ==>:
W => 444 a => 100 b => 300
M = map1.find(‘b’);
//renvoie end sin’exitepasif(M == map1.end())
cout << " le char b n'existe pas !!" << endl;
else map1.erase(G);
P.A._IG Cha- 166
M. BENJELLOUN : 2020-21 UMONS
#include
<iostream>
#include
<string>
#include <map>
using namespacestd;
template <class A> void Affichage(A Tab, string S) { typename A::iterator it;
cout << S << " dans Affichage :" << endl;
for(it=Tab.begin(); it!=Tab.end(); it++)
cout<< S<<"["<< it->first<< "] = "<< it->second<< " et \n"; // ou (*it)->first }
void main() {
map<string,int> M;
// Pour chaque clé correspondra une seule valeur associée.M["Tintin"] = 2;
M.insert(make_pair("Asterix",5));
M.insert(make_pair("Obelix",9));
M.insert(make_pair("Obelix",9));
M.insert(make_pair("Obelix",7));
Affichage(M, " M");
}
Affichage :
M dans Affichage : M[Asterix] = 5 et M[Obelix] = 9 et M[Tintin] = 2 et
Le tableau associatif Map
cout<< "Affichage hors boucle : "<< endl;
cout<< "M[Obelix] : "<< M["Obelix"] << " et \n";
cout<< "M[Tintin] : "<< M["Tintin"] << endl; Affichage hors boucle :
M[Obelix] : 9 et M[Tintin] : 2
P.A._IG Cha- 167
M. BENJELLOUN : 2020-21 UMONS
#include<iostream>
#include<string>
#include<map>
using namespacestd;
template<classA> voidAffichage(A Tab, string S) { typename A::iterator it;
cout << S << " dans Affichage :"<< endl;
for(it=Tab.begin(); it!=Tab.end(); it++)
cout<< S<<"["<< it->first<< "] = "<< it->second<< " et \n"; // ou (*it)->first }
void main() {
map<string,int> M;
// Pour chaque clé correspondra une seule valeur associée.M["Tintin"] = 2;
M.insert(make_pair("Asterix",5));
M.insert(make_pair("Obelix",9));
M.insert(make_pair("Obelix",9));
M.insert(make_pair("Obelix",7));
Affichage(M, " M");
cout<< "Affichage hors boucle : " << endl;
cout<< "M[Obelix] : " << M["Obelix"] << " et \n";
cout<< "M[Tintin] : " << M["Tintin"] << endl;
}
Affichage :
M dans Affichage : M[Asterix] = 5 et M[Obelix] = 9 et M[Tintin] = 2 et Affichage hors boucle :
M[Obelix] : 1
et M[Tintin] : 2Le tableau associatif Map
M["Obelix"] = 1;
P.A._IG Cha- 168
M. BENJELLOUN : 2020-21 UMONS
#include<iostream>
#include<string>
#include<map>
using namespacestd;
template<classA> voidAffichage(A Tab, string S) { typename A::iterator it;
cout << S << " dans Affichage :"<< endl;
for(it=Tab.begin(); it!=Tab.end(); it++)
cout<< S<<"["<< it->first<< "] = "<< it->second<< " et \n"; // ou (*it)->first }
void main() {
multimap<string,int> M;
// Pour chaque clé correspondra une seule valeur associée.M["Tintin"] = 2;
M.insert(make_pair("Asterix",5));
M.insert(make_pair("Obelix",9));
M.insert(make_pair("Obelix",9));
M.insert(make_pair("Obelix",7));
Affichage(M, "M");
cout<< "Affichage hors boucle : " << endl;
cout<< "M[Obelix] : " << M["Obelix"] << " et \n";
cout<< "M[Tintin] : " << M["Tintin"] << endl;
}
Le tableau associatif Muti Map
M["Obelix"] = 1;
// error C2676: '[' binaire :
'std::multimap<_Kty,_Ty>' ne définit pas cet opérateur
#include ...
struct Etud { int id;
string name;
};
voidmain() { map<int, Etud> Stud;
map<int, Etud>::iterator it;
Stud[124].id = 1;
Stud[124].name = " Obelix";
Stud[12].id = 5;
Stud[12].name = " Asterix";
Stud[120].id = 7;
Stud[120].name = " Tintin";
Stud[12].id = 9;
Stud[12].name = " Ass_TRix"; // écrase ancien Stud[12]
for(it=Stud.begin(); it!=Stud.end(); it++ )
cout<< it->first << " "<< it->second.id << " "<< it->second.name << endl;
system("pause");
}
Le tableau associatif Map
Affichage :
124 1 Obelix
12 5 Asterix
120 7 Tintin
12 9 Ass_TRix
P.A._IG Cha- 170
M. BENJELLOUN : 2020-21 UMONS
#include ...
struct Etud { int id;
string name;
};
voidmain() {
multimap<int, Etud> Stud;
multimap<int, Etud>::iterator it;
int static K = 1 ;
for
(int i = 0; i < 4; i++) {
EtudE;
E.id = i + 1 ; // id = 1 E.name = "Stud"
+to_string ( K ); // Stud1 Stud.insert(make_pair( i , E ) ); // 0 1 Stud1 K++ ;
}
for(it=Stud.begin(); it!=Stud.end(); it++ )
cout<< it->first << " "<< it->second.id << " "<< it->second.name << endl;
system("pause");
}
Le tableau associatif multi Map
Affichage : 0 1 Stud1 1 2 Stud2 2 3 Stud3 3 4 Stud4
P.A._IG Cha- 171
M. BENJELLOUN : 2020-21 UMONS
#include ...
struct Etud { int id;
string name;
};
voidmain() {
multimap<int, Etud> Stud;
multimap<int, Etud>::iterator it;
int static K = 1 ;
f
or(int j = 0; j < 3; j++) {
for(int i = 0; i < 4; i++) {
Etud
E;
E.id = i + 1 ; // id = 1 E.name = "Stud"
+to_string ( K ); // Stud1 Stud.insert(make_pair( i , E ) ); // 0 1 Stud1 K++ ;
} }
for(it=Stud.begin(); it!=Stud.end(); it++ )
cout<< it->first << " "<< it->second.id << " "<< it->second.name << endl;
system("pause");
}
Le tableau associatif multi Map
Affichage :
0 1 Stud1
0 1 Stud5
0 1 Stud9
1 2 Stud2
1 2 Stud6
1 2 Stud10
2 3 Stud3
2 3 Stud7
2 3 Stud11
3 4 Stud4
3 4 Stud8
3 4 Stud12
P.A._IG Cha- 172
M. BENJELLOUN : 2020-21 UMONS
.first .second
• Affichage direct depuis la Map / MultiMap de toutes les instances de la clé ‘Key2’ :
– .first fournit la "lower bound"
– .second la "upper bound"
equal_range(clé)
Le tableau associatif multi Map
#include ...
struct Etud { int id;
string name;
};
voidmain() {
multimap<int, Etud> Stud;
i
Le tableau associatif multi Map
Stud : Key Stud 0 1 Stud1 0 1 Stud5 0 1 Stud9 1 2 Stud2 1 2 Stud6 1 2 Stud10 2 3 Stud3 2 3 Stud7 2 3 Stud11 3 4 Stud4 3 4 Stud8 3 4 Stud12
Affichage:
1 2 Stud2
1 2 Stud6
1 2 Stud10
P.A._IG Cha- 174
M. BENJELLOUN : 2020-21 UMONS
#include ...
struct Etud { int id;
string name;
};
voidmain() {
multimap<int, Etud> Stud;
int
Key = 1;
if
(Stud.count(Key) == 0)
cout << "Cette clé n'est pas dans la base de données \n"
<<endl;
else
{
for(it =Stud.equal_range(Key).first; it !=Stud.equal_range(Key+1).second; it++) cout<< it->first << " "<< it->second.id << " "<< it->second.name << endl;
Le tableau associatif multi Map
Affichage:
1 2 Stud2 1 2 Stud6 1 2 Stud10 2 3 Stud3 2 3 Stud7 2 3 Stud11 Stud :
Key Stud 0 1 Stud1 0 1 Stud5 0 1 Stud9 1 2 Stud2 1 2 Stud6 1 2 Stud10 2 3 Stud3 2 3 Stud7 2 3 Stud11 3 4 Stud4 3 4 Stud8 3 4 Stud12