QStack 類

template <typename T> class QStack

The QStack class is a template class that provides a stack. 更多...

頭: #include <QStack>
qmake: QT += core
繼承: QVector

注意: 此類的所有函數 可重入 .

公共函數

T pop ()
void push (const T & t )
void swap (QStack<T> & other )
T & top ()
const T & top () const

詳細描述

QStack<T> is one of Qt's generic 容器類 . It implements a stack data structure for items of a same type.

A stack is a last in, first out (LIFO) structure. Items are added to the top of the stack using push () and retrieved from the top using pop ()。 top () function provides access to the topmost item without removing it.

範例:

    QStack<int> stack;
    stack.push(1);
    stack.push(2);
    stack.push(3);
    while (!stack.isEmpty())
        cout << stack.pop() << Qt::endl;
					

The example will output 3, 2, 1 in that order.

QStack inherits from QVector 。所有 QVector 's functionality also applies to QStack. For example, you can use isEmpty () to test whether the stack is empty, and you can traverse a QStack using QVector 的迭代器類 (例如: QVectorIterator ). But in addition, QStack provides three convenience functions that make it easy to implement LIFO semantics: push (), pop (),和 top ().

QStack's value type must be an 可賦值數據類型 . This covers most data types that are commonly used, but the compiler won't let you, for example, store a QWidget 作為值;取而代之,存儲 QWidget *.

另請參閱 QVector and QQueue .

成員函數文檔編製

T QStack:: pop ()

Removes the top item from the stack and returns it. This function assumes that the stack isn't empty.

另請參閱 top (), push (),和 isEmpty ().

void QStack:: push (const T & t )

Adds element t to the top of the stack.

這如同 QVector::append ().

另請參閱 pop () 和 top ().

void QStack:: swap ( QStack < T > & other )

Swaps stack other with this stack. This operation is very fast and never fails.

該函數在 Qt 4.8 引入。

T &QStack:: top ()

Returns a reference to the stack's top item. This function assumes that the stack isn't empty.

這如同 QVector::last ().

另請參閱 pop (), push (),和 isEmpty ().

const T &QStack:: top () const

這是重載函數。

另請參閱 pop () 和 push ().