Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
doc_src_containers.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
6{
7public:
10
12
13private:
14 QString myName;
15 QDate myDateOfBirth;
16};
18
20QList<QString> list = {"A", "B", "C", "D"};
21for (const auto &item : list) {
22 ...
23}
25
27QList<QString> list = {"A", "B", "C", "D"};
28for (const auto &item : std::as_const(list)) {
29 ...
30}
32
34QList<QString> list = {"A", "B", "C", "D"};
35for (qsizetype i = 0; i < list.size(); ++i) {
36 const auto &item = list.at(i);
37 ...
38}
40
42QList<QString> list = {"A", "B", "C", "D"};
43
44QListIterator<QString> i(list);
45while (i.hasNext())
46 QString s = i.next();
48
49
51QListIterator<QString> i(list);
52i.toBack();
53while (i.hasPrevious())
54 QString s = i.previous();
56
57
59QMutableListIterator<int> i(list);
60while (i.hasNext()) {
61 if (i.next() % 2 != 0)
62 i.remove();
63}
65
66
68QMutableListIterator<int> i(list);
69i.toBack();
70while (i.hasPrevious()) {
71 if (i.previous() % 2 != 0)
72 i.remove();
73}
75
76
78QMutableListIterator<int> i(list);
79while (i.hasNext()) {
80 if (i.next() > 128)
81 i.setValue(128);
82}
84
85
87QMutableListIterator<int> i(list);
88while (i.hasNext())
89 i.next() *= 2;
91
92
95 {"Paris", "France"},
96 {"Guatemala City", "Guatemala"},
97 {"Mexico City", "Mexico"},
98 {"Moscow", "Russia"}
99};
100...
101
102QMutableMapIterator<QString, QString> i(map);
103while (i.hasNext()) {
104 if (i.next().key().endsWith("City"))
105 i.remove();
106}
108
109
113
114QMapIterator<int, QWidget *> i(map);
115while (i.hasNext()) {
116 i.next();
117 hash.insert(i.key(), i.value());
118}
120
121
123QMutableMapIterator<int, QWidget *> i(map);
124while (i.findNext(widget))
125 i.remove();
127
128
130QList<QString> list = {"A", "B", "C", "D"};
131
132for (auto i = list.begin(), end = list.end(); i != end; ++i)
133 *i = (*i).toLower();
135
136
138QList<QString> list = {"A", "B", "C", "D"};
139
140for (auto i = list.rbegin(), rend = list.rend(); i != rend; ++i)
141 *i = i->toLower();
143
144
146for (auto i = list.cbegin(), end = list.cend(); i != end; ++i)
147 qDebug() << *i;
149
150
153...
154for (auto i = map.cbegin(), end = map.cend(); i != end; ++i)
155 qDebug() << i.key() << ':' << i.value();
157
158
160// RIGHT
161const QList<int> sizes = splitter->sizes();
162for (auto i = sizes.begin(), end = sizes.end(); i != end; ++i)
163 ...
164
165// WRONG
166for (auto i = splitter->sizes().begin();
167 i != splitter->sizes().end(); ++i)
168 ...
170
171
174...
175QString str;
176foreach (str, values)
177 qDebug() << str;
179
180
183...
184QListIterator<QString> i(values);
185while (i.hasNext()) {
186 QString s = i.next();
187 qDebug() << s;
188}
190
191
194...
195foreach (const QString &str, values)
196 qDebug() << str;
198
199
202...
203foreach (const QString &str, values) {
204 if (str.isEmpty())
205 break;
206 qDebug() << str;
207}
209
210
213...
214foreach (const QString &str, map.keys())
215 qDebug() << str << ':' << map.value(str);
217
218
221...
222foreach (const QString &str, map.uniqueKeys()) {
223 foreach (int i, map.values(str))
224 qDebug() << str << ':' << i;
225}
227
228
230CONFIG += no_keywords
232
233
235target_compile_definitions(my_app PRIVATE QT_NO_KEYWORDS)
237
238
239
240QString onlyLetters(const QString &in)
241{
242 QString out;
243 for (qsizetype j = 0; j < in.size(); ++j) {
244 if (in.at(j).isLetter())
245 out += in.at(j);
246 }
247 return out;
248}
250
253a.resize(100000); // make a big list filled with 0.
254
255QList<int>::iterator i = a.begin();
256// WRONG way of using the iterator i:
257b = a;
258/*
259 Now we should be careful with iterator i since it will point to shared data
260 If we do *i = 4 then we would change the shared instance (both vectors)
261 The behavior differs from STL containers. Avoid doing such things in Qt.
262*/
263
264a[0] = 5;
265/*
266 Container a is now detached from the shared data,
267 and even though i was an iterator from the container a, it now works as an iterator in b.
268 Here the situation is that (*i) == 0.
269*/
270
271b.clear(); // Now the iterator i is completely invalid.
272
273int j = *i; // Undefined behavior!
274/*
275 The data from b (which i pointed to) is gone.
276 This would be well-defined with STL containers (and (*i) == 5),
277 but with QList this is likely to crash.
278*/
280
282QList<int> list = {1, 2, 3, 4, 4, 5};
284/*
285 Will generate a QSet containing 1, 2, 3, 4, 5.
286*/
288
290QList<int> list = {2, 3, 1};
291
292std::sort(list.begin(), list.end());
293/*
294 Sort the list, now contains { 1, 2, 3 }
295*/
296
297std::reverse(list.begin(), list.end());
298/*
299 Reverse the list, now contains { 3, 2, 1 }
300*/
301
303 std::count_if(list.begin(), list.end(), [](int element) { return (element % 2 == 0); });
304/*
305 Count how many elements that are even numbers, 1
306*/
307
Employee & operator=(const Employee &other)
Employee(const Employee &other)
\inmodule QtCore \reentrant
Definition qdatetime.h:27
\inmodule QtCore
Definition qhash.h:818
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
iterator end()
Definition qlist.h:609
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
reverse_iterator rend()
Definition qlist.h:618
iterator begin()
Definition qlist.h:608
reverse_iterator rbegin()
Definition qlist.h:617
const_iterator cend() const noexcept
Definition qlist.h:614
const_iterator cbegin() const noexcept
Definition qlist.h:613
Definition qmap.h:186
T value(const Key &key, const T &defaultValue=T()) const
Definition qmap.h:356
const_iterator cend() const
Definition qmap.h:604
QList< T > values() const
Definition qmap.h:396
const_iterator cbegin() const
Definition qmap.h:600
QList< Key > keys() const
Definition qmap.h:382
Definition qset.h:18
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
QOpenGLWidget * widget
[1]
QHash< int, QWidget * > hash
[35multi]
QString str
[2]
int even_elements
QMap< QString, QString > map
[6]
QList< QString > list
[0]
i QList< QString > values
[14]
#define qDebug
[1]
Definition qlogging.h:160
GLenum GLsizei GLsizei GLint * values
[15]
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint GLuint end
GLuint GLsizei const GLuint const GLintptr const GLsizeiptr * sizes
GLuint in
GLdouble s
[6]
Definition qopenglext.h:235
ptrdiff_t qsizetype
Definition qtypes.h:70
QFuture< QSet< QChar > > set
[10]
QTextStream out(stdout)
[7]
QSharedPointer< T > other(t)
[5]
QGraphicsItem * item