Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qstack.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4/*!
5 \class QStack
6 \inmodule QtCore
7 \brief The QStack class is a template class that provides a stack.
8
9 \ingroup tools
10 \ingroup shared
11
12 \reentrant
13
14 QStack<T> is one of Qt's generic \l{container classes}. It implements
15 a stack data structure for items of a same type.
16
17 A stack is a last in, first out (LIFO) structure. Items are added
18 to the top of the stack using push() and retrieved from the top
19 using pop(). The top() function provides access to the topmost
20 item without removing it.
21
22 Example:
23
24 \snippet qstack/main.cpp 0
25
26 The example will output 3, 2, 1 in that order.
27
28 QStack inherits from QList. All of QList's functionality also
29 applies to QStack. For example, you can use isEmpty() to test
30 whether the stack is empty, and you can traverse a QStack using
31 QList's iterator classes (for example, QListIterator). But in
32 addition, QStack provides three convenience functions that make
33 it easy to implement LIFO semantics: push(), pop(), and top().
34
35 QStack's value type must be an \l{assignable data type}. This
36 covers most data types that are commonly used, but the compiler
37 won't let you, for example, store a QWidget as a value; instead,
38 store a QWidget *.
39
40 \sa QList, QQueue
41*/
42
43/*!
44 \fn template<class T> void QStack<T>::swap(QStack<T> &other)
45 \since 4.8
46 \memberswap{stack}
47*/
48
49/*!
50 \fn template<class T> void QStack<T>::push(const T& t)
51
52 Adds element \a t to the top of the stack.
53
54 This is the same as QList::append().
55
56 \sa pop(), top()
57*/
58
59/*!
60 \fn template<class T> T& QStack<T>::top()
61
62 Returns a reference to the stack's top item. This function
63 assumes that the stack isn't empty.
64
65 This is the same as QList::last().
66
67 \sa pop(), push(), isEmpty()
68*/
69
70/*!
71 \fn template<class T> const T& QStack<T>::top() const
72
73 \overload
74
75 \sa pop(), push()
76*/
77
78/*!
79 \fn template<class T> T QStack<T>::pop()
80
81 Removes the top item from the stack and returns it. This function
82 assumes that the stack isn't empty.
83
84 \sa top(), push(), isEmpty()
85*/