1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
using std :: string;
using std :: map;
using std :: cout;
using std :: endl;
using std :: pair;
/* J'utilise plutot le parmètre comme le container entier (et non map<T,string>) */
template<class C,class T> void AddValue(C& container,const T& key,const string& value)
{
container[key] = value;
}
// Attention au typename
template<class C> void Display(const C& container)
{
typename C :: const_iterator mcit = container.begin();
while(mcit!= container.end()) {
cout << " Clef : " << mcit->first << " Value : " << mcit->second << endl;
++mcit;
};
}
int main(int argc, char *argv[])
{
map<int,string> istringmap;
map<char,string> cstringmap;
map<double,string> dstringmap;
// J'ajoute les valeurs
AddValue(istringmap,1,"Un");
AddValue(istringmap,2,"Deux");
AddValue(istringmap,3,"Trois");
AddValue(istringmap,4,"Quatre");
AddValue(cstringmap,'1',"Un");
AddValue(cstringmap,'2',"Deux");
AddValue(cstringmap,'3',"Trois");
AddValue(cstringmap,'4',"Quatre");
AddValue(dstringmap,1.25,"Un,vingtcinq");
AddValue(dstringmap,2.9,"Deux,neuf");
AddValue(dstringmap,3.8,"Trois,huit");
AddValue(dstringmap,4.7,"Quatre,sept");
// Affiche
Display(istringmap); cout << endl;
Display(cstringmap); cout << endl;
Display(dstringmap); cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
} |
Partager