Aracılığıyla paylaş


Visual C++'da stack::top ve stack::empty STL işlevlerini kullanma

Bu makalede, Visual C++'da ve stack::empty STL işlevlerinin nasıl kullanılacağı stack::top gösterilmektedir. Bu makaledeki bilgiler yalnızca yönetilmeyen Visual C++ kodu için geçerlidir.

Özgün ürün sürümü: Visual C++
Özgün KB numarası: 158040

Gerekli başlık

  • <stack>

Prototip

template <class _TYPE, class _C, class _A> // Function 1
value_type &stack::top();

template <class _TYPE, class _C, class _A> // Function 2
const value_type &stack::top() const;

template <class _TYPE, class _C, class _A> // Function 3
bool stack::empty() const;

Not

Prototipteki sınıf veya parametre adları üst bilgi dosyasındaki sürümle eşleşmeyebilir. Bazıları okunabilirliği geliştirmek için değiştirildi.

Stack::top ve stack::empty işlevlerinin açıklaması

işlevi yığının top en üstteki öğesini döndürür. İşlevi çağırmadan top önce yığında bir veya daha fazla öğe olduğundan emin olmanız gerekir. İşlevin top ilk sürümü, yığının üst kısmındaki öğeye bir başvuru döndürerek değeri değiştirmenize olanak tanır. İkinci işlev, yığını yanlışlıkla değiştirmediğinizden emin olarak sabit bir başvuru döndürür.

İşlev, empty yığında öğe yoksa true döndürür. Bir veya daha fazla öğe varsa işlev false döndürür. İşlevi empty çağırmadan top önce yığında öğeler kaldığını doğrulamak için işlevini kullanmanız gerekir.

Örnek kod

//////////////////////////////////////////////////////////////////////
// Compile options needed: /GX
// StackTop&Empty.cpp : 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.
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////

#pragma warning(disable : 4786)

#include <stack>
#include <iostream>

#if _MSC_VER > 1020  // if VC++ version is > 4.2
    using namespace std; // std c++ libs implemented in std
#endif

typedef stack<int, deque<int>> STACK_INT;
void 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();
    }
}