stack::top und stack::empty

Veranschaulicht, wie die Stapel::Oben und Stapel::leer STL Features von Visual C++ verwendet.

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;

Hinweise

HinweisHinweis

Die Klasse/Parameternamen im Prototyp stimmen nicht mit der Version in der Headerdatei ab.Einige wurden geändert, um die Lesbarkeit zu verbessern.

Die Oben-Funktion gibt das oberste Element vom Stapel zurück.Sie sollten sicherstellen, dass es sich um eine oder mehrere Elemente im Stapel vorhanden sind, bevor Sie die oberste Funktion aufrufen.Die erste Version der obersten Funktion gibt einen Verweis auf das Element der Anfang des Stapels zurück und ermöglicht es, um den Wert zu ändern.Die zweite Funktion gibt einen konstanten Verweis zurück, und stellen Sie sicher, dass Sie nicht versehentlich dem Stapel ändern.Die leere Funktion gibt true zurück, wenn keine Elemente im Stapel vorhanden sind.Wenn eine oder mehrere Elemente vorhanden sind, gibt die Funktion falsezurück.Sie sollten die leere Funktion verwenden, um sicherzustellen, dass die Elemente vorhanden sind, die auf dem Stapel werden, bevor Sie die oberste Funktion aufrufen.

Beispiel

// 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();
   }
}
  

Anforderungen

Header: <stack>

Siehe auch

Konzepte

Standardvorlagenbibliotheks-Beispiele