Aracılığıyla paylaş


stack::top ve stack::empty

Nasıl kullanılacağı gösterilmiştir stack::top ve stack::empty Visual C++ stl fonksiyonları.

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;

Notlar

[!NOT]

Prototip sınıfı/parametre adları üstbilgi dosyasında sürüm eşleşmiyor.Bazıları, okumayı kolaylaştırmak için değiştirildi.

Üst yığının en üstteki öğe işlevini verir.Vardır, bir veya daha fazla öğe yığına üst işlevi çağırmadan önce emin olmanız gerekir.Üst işlevi ilk sürümü değeri değiştirmenize izin verecek yığının üst öğesi için bir başvuru verir.İkinci işlev, yanlışlıkla yığın değiştirmemenizi sağlayan sabit bir başvuru verir.Boş işlevini verir doğru yığındaki herhangi bir öğe varsa.Bir veya daha fazla öğe varsa vardır, işlevi döndürür yanlış.Yığında üstteki işlevi çağırmadan önce sol öğeleri doğrulamak için boş işlevini kullanmanız gerekir.

Örnek

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

Gereksinimler

Başlık: <stack>

Ayrıca bkz.

Kavramlar

Standart şablon kitaplığı örnekleri