stack::top e stack::empty

Viene illustrato come utilizzare stack:: parte superiore e stack:: vuoto Funzioni della libreria STL in Visual C++.

template<class _TYPE, class _C, class _A>
   value_type& stack::top( );
template<class _TYPE, class _C, class _A>
   const value_type& stack::top( ) const;
template<class _TYPE, class _C, class _A>
   bool stack::empty( ) const;

Note

[!NOTA]

La classe/nomi di parametro nel prototipo non corrisponde alla versione nel file di intestazione.alcuni sono stati modificati per migliorare la leggibilità.

parte superiore la funzione restituisce l'elemento di primo livello dello stack.È necessario assicurarsi che sono presenti uno o più elementi nello stack prima di chiamare la funzione principale.La prima versione della funzione principale restituisce un riferimento all'elemento dell'inizio dello stack, consentendo di modificare il valore.La seconda funzione restituisce un riferimento costante, garantendo l'installazione di non modificare accidentalmente lo stack.Restituisce vuoti di funzione true se non esistono elementi nello stack.Se sono presenti uno o più elementi, la funzione restituirà false.È necessario utilizzare la funzione vuota per verificare che siano presenti elementi che rimangono nello stack prima di chiamare la funzione principale.

Esempio

// StackTopEmpty.cpp
// compile with: /EHsc
// Illustrates how to use the top function to
// retrieve the last element of the controlled
// sequence. It also illustrates how to use the
// empty function to loop though the stack.
// 
// Functions:
//
//    top   :  returns the top element of the stack.
//    empty :  returns true if the stack has 0 elements.
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)
#include <stack>
#include <iostream>

using namespace std ;

typedef stack<int> STACK_INT;

int main()
{
   STACK_INT stack1;

   cout << "stack1.empty() returned " <<
      (stack1.empty()? "true": "false") << endl;  // Function 3

   cout << "stack1.push(2)" << endl;
   stack1.push(2);

   if (!stack1.empty())                           // Function 3
      cout << "stack1.top() returned " <<
      stack1.top() << endl;                       // Function 1

   cout << "stack1.push(5)" << endl;
   stack1.push(5);

   if (!stack1.empty())                           // Function 3
      cout << "stack1.top() returned " <<
      stack1.top() << endl;                       // Function 1

   cout << "stack1.push(11)" << endl;
   stack1.push(11);

   if (!stack1.empty())                           // Function 3
      cout << "stack1.top() returned " <<
      stack1.top() << endl;                       // Function 1

   // Modify the top item. Set it to 6.
   if (!stack1.empty()) {                         // Function 3
      cout << "stack1.top()=6;" << endl;
      stack1.top()=6;                             // Function 1
   }

   // Repeat until stack is empty
   while (!stack1.empty()) {                      // Function 3
      const int& t=stack1.top();                  // Function 2
      cout << "stack1.top() returned " << t << endl;
      cout << "stack1.pop()" << endl;
      stack1.pop();
   }
}
  

Requisiti

intestazione: <stack>

Vedere anche

Concetti

Esempi di una libreria di modelli standard