#include #include #include #include #include #include #include using namespace std; class Participant { string Nom; int age; public: void Affichage(); void Saisie(); int getAge() { return age; } string getNom() { return Nom; } // Tri par l'age bool operator<(Participant p) { return Nom < p.Nom; } }; template void saisie(C& c) { int n; cout << "Nombre de participants à saisir : "; cin >> n; cin.ignore(); while ((n--) > 0) { T participant; participant.Saisie(); c.insert(c.begin(), participant); } } template void affichage(C c) { for (auto p : c) { p.Affichage(); cout << endl; } cout << endl; } void affichage(set c) { for (auto a : c) { cout << a << endl; } cout << endl; } template void affichage(C c, const string name) { cout << "Affichage de " << name << endl; affichage(c); } // template bool tri_age(Participant a, Participant b) { return a.getAge() < b.getAge(); } // template bool tri_age_dec(Participant a, Participant b) { return !tri_age(a, b); } template void erase_max(A& a, B& b, F cmp, string txt) { auto ita = max_element(a.begin(), a.end(), cmp); auto itb = max_element(b.begin(), b.end(), cmp); cout << txt; if (ita != a.end() && (itb == b.end() || cmp(*itb, *ita))) { ita->Affichage(); a.erase(ita); } else if (itb != b.end() && (ita == a.end() || cmp(*ita, *itb))) { itb->Affichage(); b.erase(itb); } else { cout << "NULL"; } cout << endl; } template void erase_jeune_age(A& a, B& b) { erase_max(a, b, tri_age, "Le plus vieux = "); erase_max(a, b, tri_age_dec, "Le plus jeune = "); } template void add_allnom(A source, B& b) { for(auto a : source) { b.insert(a.getNom()); } } int menu() { int c; cout << "1. Saisie liste L1 + affichage" << endl << "2. Saisie vector V1 + affichage" << endl << "3. Efface le + jeune et le + agé (L1/V1)" << endl << "4. Tri croissant selon age (L1/V1) + affichage" << endl << "5. Conteneur de noms de L1/V1 trié+sans doublons" << endl << "6. Afficher L1 et V1" << endl; cout << "Choix : " << endl; cin >> c; cin.ignore(); return c; } int main() { list L1; vector V1; set noms; setlocale(LC_ALL, ""); while (true) { switch (menu()) { case 1: saisie, Participant>(L1); affichage(L1, "L1"); break; case 2: saisie, Participant>(V1); affichage(V1, "V1"); break; case 3: erase_jeune_age(L1, V1); affichage(L1, "L1"); affichage(V1, "V1"); break; case 4: L1.sort(tri_age); sort(V1.begin(), V1.end(), tri_age); affichage(L1, "L1"); affichage(V1, "V1"); break; case 5: add_allnom(L1, noms); add_allnom(V1, noms); affichage(noms, "Affichage du conteneur :"); break; case 6: affichage(L1, "L1"); affichage(V1, "V1"); break; default: return 0; } } } template void affiche(T t, int w) { cout << left << setw(w) << t << " "; } void Participant::Affichage() { affiche(Nom, 10); affiche(age, 4); } void Participant::Saisie() { // Saisie de du nom : via getline(); cout << "Son nom ? : "; getline(cin, Nom); // Saisie de l'age cout << "Son age ? : "; cin >> age; cin.ignore(); }