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