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
qalgorithms.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\headerfile <QtAlgorithms>
6
\inmodule QtCore
7
\title Generic Algorithms
8
\ingroup funclists
9
\keyword generic algorithms
10
11
\brief The <QtAlgorithms> header includes the generic, template-based algorithms.
12
13
Qt provides a number of global template functions in \c
14
<QtAlgorithms> that work on containers and perform small tasks to
15
make life easier, such as qDeleteAll(), which invokes \c{operator delete}
16
on all items in a given container or in a given range.
17
You can use these algorithms with any \l {container
18
class} that provides STL-style iterators, including Qt's QList,
19
QMap, and QHash classes.
20
21
Most algorithms take \l {STL-style iterators} as parameters. The
22
algorithms are generic in the sense that they aren't bound to a
23
specific iterator class; you can use them with any iterators that
24
meet a certain set of requirements.
25
26
Different algorithms can have different requirements for the
27
iterators they accept. The iterator types required are specified
28
for each algorithm. If an iterator of the wrong type is passed (for
29
example, if QList::ConstIterator is passed as an
30
\l {Output Iterators}{output iterator}), you will always get a
31
compiler error, although not necessarily a very informative one.
32
33
Some algorithms have special requirements on the value type stored
34
in the containers. For example, qDeleteAll() requires that the
35
value type is a non-const pointer type (for example, QWidget
36
*). The value type requirements are specified for each algorithm,
37
and the compiler will produce an error if a requirement isn't met.
38
39
The generic algorithms can be used on other container classes
40
than those provided by Qt and STL. The syntax of STL-style
41
iterators is modeled after C++ pointers, so it's possible to use
42
plain arrays as containers and plain pointers as iterators.
43
44
\section1 Types of Iterators
45
46
The algorithms have certain requirements on the iterator types
47
they accept, and these are specified individually for each
48
function. The compiler will produce an error if a requirement
49
isn't met.
50
51
\section2 Input Iterators
52
53
An \e{input iterator} is an iterator that can be used for reading
54
data sequentially from a container. It must provide the following
55
operators: \c{==} and \c{!=} for comparing two iterators, unary
56
\c{*} for retrieving the value stored in the item, and prefix
57
\c{++} for advancing to the next item.
58
59
The Qt containers' iterator types (const and non-const) are all
60
input iterators.
61
62
\section2 Output Iterators
63
64
An output iterator is an iterator that can be used for
65
writing data sequentially to a container or to some output
66
stream. It must provide the following operators: unary \c{*} for
67
writing a value (i.e., \c{*it = val}) and prefix \c{++} for
68
advancing to the next item.
69
70
The Qt containers' non-const iterator types are all output
71
iterators.
72
73
\section2 Forward Iterators
74
75
A \e{forward iterator} is an iterator that meets the requirements
76
of both input iterators and output iterators.
77
78
The Qt containers' non-const iterator types are all forward
79
iterators.
80
81
\section2 Bidirectional Iterators
82
83
A \e{bidirectional iterator} is an iterator that meets the
84
requirements of forward iterators but that in addition supports
85
prefix \c{--} for iterating backward.
86
87
The Qt containers' non-const iterator types are all bidirectional
88
iterators.
89
90
\section2 Random Access Iterators
91
92
The last category, \e{random access iterators}, is the most
93
powerful type of iterator. It supports all the requirements of a
94
bidirectional iterator, and supports the following operations:
95
96
\table
97
\row \li \c{i += n} \li advances iterator \c i by \c n positions
98
\row \li \c{i -= n} \li moves iterator \c i back by \c n positions
99
\row \li \c{i + n} or \c{n + i} \li returns the iterator for the item \c
100
n positions ahead of iterator \c i
101
\row \li \c{i - n} \li returns the iterator for the item \c n positions behind of iterator \c i
102
\row \li \c{i - j} \li returns the number of items between iterators \c i and \c j
103
\row \li \c{i[n]} \li same as \c{*(i + n)}
104
\row \li \c{i < j} \li returns \c true if iterator \c j comes after iterator \c i
105
\endtable
106
107
QList's non-const iterator type is random access iterator.
108
109
\sa {container classes}, <QtGlobal>
110
*/
111
112
/*!
113
\fn template <typename ForwardIterator> void qDeleteAll(ForwardIterator begin, ForwardIterator end)
114
\relates <QtAlgorithms>
115
116
Deletes all the items in the range [\a begin, \a end) using the
117
C++ \c delete operator. The item type must be a pointer type (for
118
example, \c{QWidget *}).
119
120
Example:
121
\snippet code/doc_src_qalgorithms.cpp 1
122
123
Notice that qDeleteAll() doesn't remove the items from the
124
container; it merely calls \c delete on them. In the example
125
above, we call clear() on the container to remove the items.
126
127
This function can also be used to delete items stored in
128
associative containers, such as QMap and QHash. Only the objects
129
stored in each container will be deleted by this function; objects
130
used as keys will not be deleted.
131
132
\sa {forward iterators}
133
*/
134
135
/*!
136
\fn template <typename Container> void qDeleteAll(const Container &c)
137
\relates <QtAlgorithms>
138
139
\overload
140
141
This is the same as qDeleteAll(\a{c}.begin(), \a{c}.end()).
142
*/
143
144
/*!
145
\fn uint qPopulationCount(quint8 v)
146
\relates <QtAlgorithms>
147
\since 5.2
148
149
Returns the number of bits set in \a v. This number is also called
150
the Hamming Weight of \a v.
151
*/
152
153
/*!
154
\fn uint qPopulationCount(quint16 v)
155
\relates <QtAlgorithms>
156
\since 5.2
157
\overload
158
*/
159
160
/*!
161
\fn uint qPopulationCount(quint32 v)
162
\relates <QtAlgorithms>
163
\since 5.2
164
\overload
165
*/
166
167
/*!
168
\fn uint qPopulationCount(quint64 v)
169
\relates <QtAlgorithms>
170
\since 5.2
171
\overload
172
*/
173
174
/*!
175
\fn uint qCountTrailingZeroBits(quint8 v)
176
\relates <QtAlgorithms>
177
\since 5.6
178
179
Returns the number of consecutive zero bits in \a v, when searching from the LSB.
180
For example, qCountTrailingZeroBits(1) returns 0 and qCountTrailingZeroBits(8) returns 3.
181
*/
182
183
/*!
184
\fn uint qCountTrailingZeroBits(quint16 v)
185
\relates <QtAlgorithms>
186
\since 5.6
187
\overload
188
*/
189
190
/*!
191
\fn uint qCountTrailingZeroBits(quint32 v)
192
\relates <QtAlgorithms>
193
\since 5.6
194
\overload
195
*/
196
197
/*!
198
\fn uint qCountTrailingZeroBits(quint64 v)
199
\relates <QtAlgorithms>
200
\since 5.6
201
\overload
202
*/
203
204
/*!
205
\fn uint qCountLeadingZeroBits(quint8 v)
206
\relates <QtAlgorithms>
207
\since 5.6
208
209
Returns the number of consecutive zero bits in \a v, when searching from the MSB.
210
For example, qCountLeadingZeroBits(quint8(1)) returns 7 and
211
qCountLeadingZeroBits(quint8(8)) returns 4.
212
*/
213
214
/*!
215
\fn uint qCountLeadingZeroBits(quint16 v)
216
\relates <QtAlgorithms>
217
\since 5.6
218
219
Returns the number of consecutive zero bits in \a v, when searching from the MSB.
220
For example, qCountLeadingZeroBits(quint16(1)) returns 15 and
221
qCountLeadingZeroBits(quint16(8)) returns 12.
222
*/
223
224
/*!
225
\fn uint qCountLeadingZeroBits(quint32 v)
226
\relates <QtAlgorithms>
227
\since 5.6
228
229
Returns the number of consecutive zero bits in \a v, when searching from the MSB.
230
For example, qCountLeadingZeroBits(quint32(1)) returns 31 and
231
qCountLeadingZeroBits(quint32(8)) returns 28.
232
*/
233
234
/*!
235
\fn uint qCountLeadingZeroBits(quint64 v)
236
\relates <QtAlgorithms>
237
\since 5.6
238
239
Returns the number of consecutive zero bits in \a v, when searching from the MSB.
240
For example, qCountLeadingZeroBits(quint64(1)) returns 63 and
241
qCountLeadingZeroBits(quint64(8)) returns 60.
242
*/
243
244
/*!
245
\fn template <typename InputIterator, typename Result, typename Separator = Result, typename Projection = q20::identity> Result qJoin(InputIterator first, InputIterator last, Result init, const Separator &separator, Projection p)
246
\relates <QtAlgorithms>
247
\since 6.10
248
249
Starts with initial value \a init and cumulatively adds to it each entry in
250
the range from \a first to \a last. Each entry is, before adding, mapped
251
through the projection \a p (which defaults to the identity). Between each
252
entry and the next, an optional \a separator is added. For example:
253
254
\code
255
QList<int> l = {1, 2, 3};
256
QString res = qJoin(l.cbegin(), l.cend(), QString(), u" / ",
257
[](int n) { return QString::number(2 * n);});
258
// res == "2 / 4 / 6"
259
\endcode
260
261
\note \c{q20::identity} is a C++17 backport of C++20's
262
\l{https://en.cppreference.com/w/cpp/utility/functional/identity}{\c{std::identity}}.
263
*/
qtbase
src
corelib
tools
qalgorithms.qdoc
Generated on Sun Mar 9 2025 01:10:24 for Qt by
1.13.2