Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses
5
8{
9public:
12
14
15private:
16 QString myName;
17 QDate myDateOfBirth;
18};
20
22QList<QString> list = {"A", "B", "C", "D"};
23for (const auto &item : list) {
24 ...
25}
27
29QList<QString> list = {"A", "B", "C", "D"};
30for (const auto &item : std::as_const(list)) {
31 ...
32}
34
36QList<QString> list = {"A", "B", "C", "D"};
37for (qsizetype i = 0; i < list.size(); ++i) {
38 const auto &item = list.at(i);
39 ...
40}
42
44QList<QString> list = {"A", "B", "C", "D"};
45
46QListIterator<QString> i(list);
47while (i.hasNext())
48 QString s = i.next();
50
51
53QListIterator<QString> i(list);
54i.toBack();
55while (i.hasPrevious())
56 QString s = i.previous();
58
59
61QMutableListIterator<int> i(list);
62while (i.hasNext()) {
63 if (i.next() % 2 != 0)
64 i.remove();
65}
67
68
70QMutableListIterator<int> i(list);
71i.toBack();
72while (i.hasPrevious()) {
73 if (i.previous() % 2 != 0)
74 i.remove();
75}
77
78
80QMutableListIterator<int> i(list);
81while (i.hasNext()) {
82 if (i.next() > 128)
83 i.setValue(128);
84}
86
87
89QMutableListIterator<int> i(list);
90while (i.hasNext())
91 i.next() *= 2;
93
94
96QMap<QString, QString> map = {
97 {"Paris", "France"},
98 {"Guatemala City", "Guatemala"},
99 {"Mexico City", "Mexico"},
100 {"Moscow", "Russia"}
101};
102...
103
104QMutableMapIterator<QString, QString> i(map);
105while (i.hasNext()) {
106 if (i.next().key().endsWith("City"))
107 i.remove();
108}
110
111
113QMap<int, QWidget *> map;
114QHash<int, QWidget *> hash;
115
116QMapIterator<int, QWidget *> i(map);
117while (i.hasNext()) {
118 i.next();
119 hash.insert(i.key(), i.value());
120}
122
123
125QMutableMapIterator<int, QWidget *> i(map);
126while (i.findNext(widget))
127 i.remove();
129
130
132QList<QString> list = {"A", "B", "C", "D"};
133
134for (auto i = list.begin(), end = list.end(); i != end; ++i)
135 *i = (*i).toLower();
137
138
140QList<QString> list = {"A", "B", "C", "D"};
141
142for (auto i = list.rbegin(), rend = list.rend(); i != rend; ++i)
143 *i = i->toLower();
145
146
148for (auto i = list.cbegin(), end = list.cend(); i != end; ++i)
149 qDebug() << *i;
151
152
154QMap<int, int> map;
155...
156for (auto i = map.cbegin(), end = map.cend(); i != end; ++i)
157 qDebug() << i.key() << ':' << i.value();
159
160
162// RIGHT
163const QList<int> sizes = splitter->sizes();
164for (auto i = sizes.begin(), end = sizes.end(); i != end; ++i)
165 ...
166
167// WRONG
168for (auto i = splitter->sizes().begin();
169 i != splitter->sizes().end(); ++i)
170 ...
172
173
175QList<QString> values;
176...
177QString str;
178foreach (str, values)
179 qDebug() << str;
181
182
184QList<QString> values;
185...
186QListIterator<QString> i(values);
187while (i.hasNext()) {
188 QString s = i.next();
189 qDebug() << s;
190}
192
193
195QList<QString> values;
196...
197foreach (const QString &str, values)
198 qDebug() << str;
200
201
203QList<QString> values;
204...
205foreach (const QString &str, values) {
206 if (str.isEmpty())
207 break;
208 qDebug() << str;
209}
211
212
214QMap<QString, int> map;
215...
216foreach (const QString &str, map.keys())
217 qDebug() << str << ':' << map.value(str);
219
220
222QMultiMap<QString, int> map;
223...
224foreach (const QString &str, map.uniqueKeys()) {
225 foreach (int i, map.values(str))
226 qDebug() << str << ':' << i;
227}
229
230
232CONFIG += no_keywords
234
235
237target_compile_definitions(my_app PRIVATE QT_NO_KEYWORDS)
239
240
241
242QString onlyLetters(const QString &in)
243{
244 QString out;
245 for (qsizetype j = 0; j < in.size(); ++j) {
246 if (in.at(j).isLetter())
247 out += in.at(j);
248 }
249 return out;
250}
252
254QList<int> a, b;
255a.resize(100000); // make a big list filled with 0.
256
257QList<int>::iterator i = a.begin();
258// WRONG way of using the iterator i:
259b = a;
260/*
261 Now we should be careful with iterator i since it will point to shared data
262 If we do *i = 4 then we would change the shared instance (both vectors)
263 The behavior differs from STL containers. Avoid doing such things in Qt.
264*/
265
266a[0] = 5;
267/*
268 Container a is now detached from the shared data,
269 and even though i was an iterator from the container a, it now works as an iterator in b.
270 Here the situation is that (*i) == 0.
271*/
272
273b.clear(); // Now the iterator i is completely invalid.
274
275int j = *i; // Undefined behavior!
276/*
277 The data from b (which i pointed to) is gone.
278 This would be well-defined with STL containers (and (*i) == 5),
279 but with QList this is likely to crash.
280*/
282
284QList<int> list = {1, 2, 3, 4, 4, 5};
285QSet<int> set(list.cbegin(), list.cend());
286/*
287 Will generate a QSet containing 1, 2, 3, 4, 5.
288*/
290
292QList<int> list = {2, 3, 1};
293
294std::sort(list.begin(), list.end());
295/*
296 Sort the list, now contains { 1, 2, 3 }
297*/
298
299std::reverse(list.begin(), list.end());
300/*
301 Reverse the list, now contains { 3, 2, 1 }
302*/
303
305 std::count_if(list.begin(), list.end(), [](int element) { return (element % 2 == 0); });
306/*
307 Count how many elements that are even numbers, 1
308*/
309
Employee & operator=(const Employee &other)
Employee(const Employee &other)
\inmodule QtCore \reentrant
Definition qdatetime.h:29
qsizetype size() const noexcept
Definition qlist.h:397
iterator end()
Definition qlist.h:626
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
reverse_iterator rend()
Definition qlist.h:635
iterator begin()
Definition qlist.h:625
reverse_iterator rbegin()
Definition qlist.h:634
const_iterator cend() const noexcept
Definition qlist.h:631
const_iterator cbegin() const noexcept
Definition qlist.h:630
T value(const Key &key, const T &defaultValue=T()) const
Definition qmap.h:357
const_iterator cend() const
Definition qmap.h:605
QList< T > values() const
Definition qmap.h:397
const_iterator cbegin() const
Definition qmap.h:601
QList< Key > keys() const
Definition qmap.h:383
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
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:164
GLenum GLsizei GLsizei GLint * values
[15]
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint GLuint end
GLdouble s
[6]
Definition qopenglext.h:235
GLuint GLsizei const GLuint const GLintptr const GLsizeiptr * sizes
GLuint in
ptrdiff_t qsizetype
Definition qtypes.h:165
QFuture< QSet< QChar > > set
[10]
QTextStream out(stdout)
[7]
QSharedPointer< T > other(t)
[5]
QGraphicsItem * item