QStack Class

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

额外继承成员

  • 2 static public members inherited from QVector

详细描述

QStack class is a template class that provides a stack.

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() << endl;
					

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

QStack 继承自 QVector . All of 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 使用 QVector 's iterator classes (for example, 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 as a value; instead, store a 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 ().