C++: Exemple explicite d’utilisation de ‘stack’ avec une méthode générique

Author:

 méthode
Download

#include 
#include 
using namespace std;
template

void afficher_stack(stack& qStack);

 int main()
 {

 // Création d'un stack de int
   stack intStack;
   cout << "nLe stack 'intStack' crée:n";
   /* Afficher les propriétés de stack crée*/
   afficher_stack(intStack);

   /* Ajout des éléments dans le stack */
  for (unsigned int i = 0; i < 10; ++i)
	  intStack.push(i * 2);

 /* Afficher les propriétés de stack*/
	cout << "Après Ajout, intStack:n";
	afficher_stack(intStack);

	/* Modifier l'élément qui est à la tête du stack*/
	intStack.top() = 80;
	cout << "Après la modification, intStack:n";
	afficher_stack(intStack);

	 /* Afficher les élément du stack*/
	cout << "Les contenu de l'adaptateur 'satck'(Mode LIFO):n";
	while (!intStack.empty())
	{
	cout << intStack.top() << ", ";
	intStack.pop();
	}
	cout << "\n\n";
	return 0;
 }
/* Fonction générique d'affichage des propriétés d'un stack */
template  void afficher_stack(stack& qStack)
	{
	cout << "Taille = " << qStack.size();
	if (!qStack.empty())
	cout << "tête du stack = " << qStack.top();
	cout << "nn";
	}

Leave a Reply

Your email address will not be published. Required fields are marked *