QStack Class
template <typename T> class QStackThe QStack class is a template class that provides a stack. More...
Header: | #include <QStack> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
Inherits: | QList |
- List of all members, including inherited members
- QStack is part of Implicitly Shared Classes.
Note: All functions in this class are reentrant.
Public Functions
Detailed Description
QStack<T> is one of Qt's generic container classes. 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(). The top() function provides access to the topmost item without removing it.
Example:
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 QList. All of QList'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 QList's iterator classes (for example, QListIterator). But in addition, QStack provides three convenience functions that make it easy to implement LIFO semantics: push(), pop(), and top().
QStack's value type must be an assignable data type. 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 *.
Member Function Documentation
T QStack::pop()
Removes the top item from the stack and returns it. This function assumes that the stack isn't empty.
See also top(), push(), and isEmpty().
void QStack::push(const T &t)
Adds element t to the top of the stack.
This is the same as QList::append().
void QStack::swap(QStack<T> &other)
Swaps this stack with other. This operation is very fast and never fails.
T &QStack::top()
Returns a reference to the stack's top item. This function assumes that the stack isn't empty.
This is the same as QList::last().
See also pop(), push(), and isEmpty().
const T &QStack::top() const
This is an overloaded function.
© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.