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 \ingroup containers
12
13 \reentrant
14
15 QStack<T> is one of Qt's generic \l{container classes}, where
16 \a T specifies the element type. It implements a stack data structure
17 for items of the same type.
18
19 A stack is a last in, first out (LIFO) structure. Items are added
20 to the top of the stack using push() and retrieved from the top
21 using pop(). The top() function provides access to the topmost
22 item without removing it.
23
24 Example:
25
26 \snippet qstack/main.cpp 0
27
28 The example will output 3, 2, 1 in that order.
29
30 QStack inherits from QList. All of QList's functionality also
31 applies to QStack. For example, you can use isEmpty() to test
32 whether the stack is empty, and you can traverse a QStack using
33 QList's iterator classes (for example, QListIterator). But in
34 addition, QStack provides three convenience functions that make
35 it easy to implement LIFO semantics: push(), pop(), and top().
36
37 QStack's value type must be an \l{assignable data type}. This
38 covers most data types that are commonly used, but the compiler
39 won't let you, for example, store a QWidget as a value; instead,
40 store a QWidget *.
41
42 \sa QList, QQueue
43*/
44
45/*!
46 \fn template<class T> void QStack<T>::swap(QStack<T> &other)
47 \since 4.8
48 \memberswap{stack}
49*/
50
51/*!
52 \fn template<class T> void QStack<T>::push(const T& t)
53
54 Adds element \a t to the top of the stack.
55
56 This is the same as QList::append().
57
58 \sa pop(), top()
59*/
60
61/*!
62 \fn template<class T> void QStack<T>::push(T&& t)
63 \since 6.11
64
65 Adds element \a t to the top of the stack.
66
67 This is the same as QList::append().
68
69 \sa pop(), top()
70*/
71
72/*!
73 \fn template<class T> T& QStack<T>::top()
74
75 Returns a reference to the stack's top item. This function
76 assumes that the stack isn't empty.
77
78 This is the same as QList::last().
79
80 \sa pop(), push(), isEmpty()
81*/
82
83/*!
84 \fn template<class T> const T& QStack<T>::top() const
85
86 \overload
87
88 \sa pop(), push()
89*/
90
91/*!
92 \fn template<class T> T QStack<T>::pop()
93
94 Removes the top item from the stack and returns it. This function
95 assumes that the stack isn't empty.
96
97 \sa top(), push(), isEmpty()
98*/