QtBase  v6.3.1
doc_src_containers.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
52 class Employee
53 {
54 public:
55  Employee() {}
57 
59 
60 private:
61  QString myName;
62  QDate myDateOfBirth;
63 };
65 
66 
69 list << "A" << "B" << "C" << "D";
70 
71 QListIterator<QString> i(list);
72 while (i.hasNext())
73  QString s = i.next();
75 
76 
78 QListIterator<QString> i(list);
79 i.toBack();
80 while (i.hasPrevious())
81  QString s = i.previous();
83 
84 
86 QMutableListIterator<int> i(list);
87 while (i.hasNext()) {
88  if (i.next() % 2 != 0)
89  i.remove();
90 }
92 
93 
95 QMutableListIterator<int> i(list);
96 i.toBack();
97 while (i.hasPrevious()) {
98  if (i.previous() % 2 != 0)
99  i.remove();
100 }
102 
103 
105 QMutableListIterator<int> i(list);
106 while (i.hasNext()) {
107  if (i.next() > 128)
108  i.setValue(128);
109 }
111 
112 
114 QMutableListIterator<int> i(list);
115 while (i.hasNext())
116  i.next() *= 2;
118 
119 
122 map.insert("Paris", "France");
123 map.insert("Guatemala City", "Guatemala");
124 map.insert("Mexico City", "Mexico");
125 map.insert("Moscow", "Russia");
126 ...
127 
128 QMutableMapIterator<QString, QString> i(map);
129 while (i.hasNext()) {
130  if (i.next().key().endsWith("City"))
131  i.remove();
132 }
134 
135 
139 
140 QMapIterator<int, QWidget *> i(map);
141 while (i.hasNext()) {
142  i.next();
143  hash.insert(i.key(), i.value());
144 }
146 
147 
149 QMutableMapIterator<int, QWidget *> i(map);
150 while (i.findNext(widget))
151  i.remove();
153 
154 
157 list << "A" << "B" << "C" << "D";
158 
160 for (i = list.begin(); i != list.end(); ++i)
161  *i = (*i).toLower();
163 
164 
167 list << "A" << "B" << "C" << "D";
168 
170 for (i = list.rbegin(); i != list.rend(); ++i)
171  *i = i->toLower();
173 
174 
177 for (i = list.constBegin(); i != list.constEnd(); ++i)
178  qDebug() << *i;
180 
181 
184 ...
185 QMap<int, int>::const_iterator i;
186 for (i = map.constBegin(); i != map.constEnd(); ++i)
187  qDebug() << i.key() << ':' << i.value();
189 
190 
192 // RIGHT
193 const QList<int> sizes = splitter->sizes();
195 for (i = sizes.begin(); i != sizes.end(); ++i)
196  ...
197 
198 // WRONG
199 QList<int>::const_iterator i;
200 for (i = splitter->sizes().begin();
201  i != splitter->sizes().end(); ++i)
202  ...
204 
205 
207 QList<QString> values;
208 ...
209 QString str;
210 foreach (str, values)
211  qDebug() << str;
213 
214 
217 ...
218 QListIterator<QString> i(values);
219 while (i.hasNext()) {
220  QString s = i.next();
221  qDebug() << s;
222 }
224 
225 
228 ...
229 foreach (const QString &str, values)
230  qDebug() << str;
232 
233 
236 ...
237 foreach (const QString &str, values) {
238  if (str.isEmpty())
239  break;
240  qDebug() << str;
241 }
243 
244 
247 ...
248 foreach (const QString &str, map.keys())
249  qDebug() << str << ':' << map.value(str);
251 
252 
255 ...
256 foreach (const QString &str, map.uniqueKeys()) {
257  foreach (int i, map.values(str))
258  qDebug() << str << ':' << i;
259 }
261 
262 
265  ...
266 }
268 
269 
271 CONFIG += no_keywords
273 
274 
276 target_compile_definitions(my_app PRIVATE QT_NO_KEYWORDS)
278 
279 
280 QString onlyLetters(const QString &in)
282 {
283  QString out;
284  for (int j = 0; j < in.size(); ++j) {
285  if (in[j].isLetter())
286  out += in[j];
287  }
288  return out;
289 }
291 
294 a.resize(100000); // make a big list filled with 0.
295 
296 QList<int>::iterator i = a.begin();
297 // WRONG way of using the iterator i:
298 b = a;
299 /*
300  Now we should be careful with iterator i since it will point to shared data
301  If we do *i = 4 then we would change the shared instance (both vectors)
302  The behavior differs from STL containers. Avoid doing such things in Qt.
303 */
304 
305 a[0] = 5;
306 /*
307  Container a is now detached from the shared data,
308  and even though i was an iterator from the container a, it now works as an iterator in b.
309  Here the situation is that (*i) == 0.
310 */
311 
312 b.clear(); // Now the iterator i is completely invalid.
313 
314 int j = *i; // Undefined behavior!
315 /*
316  The data from b (which i pointed to) is gone.
317  This would be well-defined with STL containers (and (*i) == 5),
318  but with QList this is likely to crash.
319 */
321 
323 QList<int> list { 1, 2, 3, 4, 4, 5 };
325 /*
326  Will generate a QSet containing 1, 2, 3, 4, 5.
327 */
329 
331 QList<int> list { 2, 3, 1 };
332 
333 std::sort(list.begin(), list.end());
334 /*
335  Sort the list, now contains { 1, 2, 3 }
336 */
337 
338 std::reverse(list.begin(), list.end());
339 /*
340  Reverse the list, now contains { 3, 2, 1 }
341 */
342 
344  std::count_if(list.begin(), list.end(), [](int element) { return (element % 2 == 0); });
345 /*
346  Count how many elements that are even numbers, 1
347 */
348 
Employee(const Employee &other)
Employee & operator=(const Employee &other)
The QDate class provides date functions.
Definition: qdatetime.h:64
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qhash.h:773
iterator end()
Definition: qlist.h:624
const_iterator constBegin() const noexcept
Definition: qlist.h:630
reverse_iterator rend()
Definition: qlist.h:633
iterator begin()
Definition: qlist.h:623
reverse_iterator rbegin()
Definition: qlist.h:632
const_iterator constEnd() const noexcept
Definition: qlist.h:631
iterator insert(const Key &key, const T &value)
Definition: qmap.h:719
T value(const Key &key, const T &defaultValue=T()) const
Definition: qmap.h:392
QList< T > values() const
Definition: qmap.h:432
QList< Key > keys() const
Definition: qmap.h:418
QMap()=default
const_iterator constBegin() const
Definition: qmap.h:635
const_iterator constEnd() const
Definition: qmap.h:639
The QString class provides a Unicode character string.
Definition: qstring.h:388
bool isEmpty() const
Definition: qstring.h:1216
QOpenGLWidget * widget
[1]
QSet< int > set(list.begin(), list.end())
QHash< int, QWidget * > hash
[35multi]
QMap< QString, QString > map
[6]
QString str
[2]
list<< "A"<< "B"<< "C"<< "D";QList< QString >::iterator i
[11]
int even_elements
QList< QString > list
[0]
forever
[20]
QList< QString > values
[15]
typename C::const_iterator const_iterator
#define QString()
Definition: parse-defines.h:51
#define qDebug
[1]
Definition: qlogging.h:177
GLenum GLsizei GLsizei GLint * values
[16]
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint GLsizei const GLuint const GLintptr const GLsizeiptr * sizes
Definition: qopenglext.h:2587
GLuint in
Definition: qopenglext.h:8870
GLdouble s
[6]
Definition: qopenglext.h:235
QTextStream out(stdout)
[7]
QSharedPointer< T > other(t)
[5]
#define QT_NO_KEYWORDS