QtBase  v6.3.1
src_corelib_tools_qmap.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 
54 
55 
57 map["one"] = 1;
58 map["three"] = 3;
59 map["seven"] = 7;
61 
62 
64 map.insert("twelve", 12);
66 
67 
69 int num1 = map["thirteen"];
70 int num2 = map.value("thirteen");
72 
73 
75 int timeout = 30;
76 if (map.contains("TIMEOUT"))
77  timeout = map.value("TIMEOUT");
79 
80 
82 int timeout = map.value("TIMEOUT", 30);
84 
85 
87 // WRONG
89 ...
90 for (int i = 0; i < 1000; ++i) {
91  if (map[i] == okButton)
92  cout << "Found button at index " << i << Qt::endl;
93 }
95 
96 
98 QMapIterator<QString, int> i(map);
99 while (i.hasNext()) {
100  i.next();
101  cout << i.key() << ": " << i.value() << Qt::endl;
102 }
104 
105 
108 while (i != map.constEnd()) {
109  cout << i.key() << ": " << i.value() << Qt::endl;
110  ++i;
111 }
113 
114 
116 map.insert("plenty", 100);
117 map.insert("plenty", 2000);
118 // map.value("plenty") == 2000
120 
121 
124 ...
125 foreach (int value, map)
126  cout << value << Qt::endl;
128 
129 
131 #ifndef EMPLOYEE_H
132 #define EMPLOYEE_H
133 
134 class Employee
135 {
136 public:
137  Employee() {}
138  Employee(const QString &name, QDate dateOfBirth);
139  ...
140 
141 private:
142  QString myName;
143  QDate myDateOfBirth;
144 };
145 
146 inline bool operator<(const Employee &e1, const Employee &e2)
147 {
148  if (e1.name() != e2.name())
149  return e1.name() < e2.name();
150  return e1.dateOfBirth() < e2.dateOfBirth();
151 }
152 
153 #endif // EMPLOYEE_H
155 
156 
159 ...
160 QMap<QString, int>::const_iterator i = map.find("HDR");
161 while (i != map.end() && i.key() == "HDR") {
162  cout << i.value() << Qt::endl;
163  ++i;
164 }
166 
167 
170 map.insert(1, "one");
171 map.insert(5, "five");
172 map.insert(10, "ten");
173 
174 map.lowerBound(0); // returns iterator to (1, "one")
175 map.lowerBound(1); // returns iterator to (1, "one")
176 map.lowerBound(2); // returns iterator to (5, "five")
177 map.lowerBound(10); // returns iterator to (10, "ten")
178 map.lowerBound(999); // returns end()
180 
181 
184 ...
187 while (i != upperBound) {
188  cout << i.value() << Qt::endl;
189  ++i;
190 }
192 
193 
196 map.insert(1, "one");
197 map.insert(5, "five");
198 map.insert(10, "ten");
199 
200 map.upperBound(0); // returns iterator to (1, "one")
201 map.upperBound(1); // returns iterator to (5, "five")
202 map.upperBound(2); // returns iterator to (5, "five")
203 map.upperBound(10); // returns end()
204 map.upperBound(999); // returns end()
206 
207 
210 map.insert("January", 1);
211 map.insert("February", 2);
212 ...
213 map.insert("December", 12);
214 
216 for (i = map.begin(); i != map.end(); ++i)
217  cout << i.key() << ": " << i.value() << Qt::endl;
219 
220 
223 for (i = map.begin(); i != map.end(); ++i)
224  i.value() += 2;
226 
227 
230 while (i != map.end()) {
231  if (i.key().startsWith('_'))
232  i = map.erase(i);
233  else
234  ++i;
235 }
237 
238 
241 while (i != map.end()) {
243  ++i;
244  if (prev.key().startsWith('_'))
245  map.erase(prev);
246 }
248 
249 
251 // WRONG
252 while (i != map.end()) {
253  if (i.key().startsWith('_'))
254  map.erase(i);
255  ++i;
256 }
258 
259 
261 if (i.key() == "Hello")
262  i.value() = "Bonjour";
264 
265 
268 map.insert("January", 1);
269 map.insert("February", 2);
270 ...
271 map.insert("December", 12);
272 
274 for (i = map.constBegin(); i != map.constEnd(); ++i)
275  cout << i.key() << ": " << i.value() << Qt::endl;
277 
278 
280 QMultiMap<QString, int> map1, map2, map3;
281 
282 map1.insert("plenty", 100);
283 map1.insert("plenty", 2000);
284 // map1.size() == 2
285 
286 map2.insert("plenty", 5000);
287 // map2.size() == 1
288 
289 map3 = map1 + map2;
290 // map3.size() == 3
292 
293 
295 QList<int> values = map.values("plenty");
296 for (int i = 0; i < values.size(); ++i)
297  cout << values.at(i) << Qt::endl;
299 
300 
303 while (i != map.end() && i.key() == "plenty") {
304  cout << i.value() << Qt::endl;
305  ++i;
306 }
308 
311  cout << "The key: " << it.key() << Qt::endl
312  cout << "The value: " << it.value() << Qt::endl;
313  cout << "Also the value: " << (*it) << Qt::endl;
314 }
316 
318 // Inefficient, keys() is expensive
319 QList<int> keys = map.keys();
320 int numPrimes = std::count_if(map.cbegin(), map.cend(), isPrimeNumber);
321 qDeleteAll(map2.keys());
322 
323 // Efficient, no memory allocation needed
324 int numPrimes = std::count_if(map.keyBegin(), map.keyEnd(), isPrimeNumber);
325 qDeleteAll(map2.keyBegin(), map2.keyEnd());
QString name() const
[5] //! [6]
Definition: employee.h:96
Employee(const QString &name, QDate dateOfBirth)
The QDate class provides date functions.
Definition: qdatetime.h:64
bool operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
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
iterator erase(const_iterator it)
Definition: qmap.h:650
QList< Key > keys() const
Definition: qmap.h:418
bool contains(const Key &key) const
Definition: qmap.h:376
const_iterator cend() const
Definition: qmap.h:640
QMap()=default
const_iterator cbegin() const
Definition: qmap.h:636
iterator find(const Key &key)
Definition: qmap.h:672
iterator lowerBound(const Key &key)
Definition: qmap.h:691
iterator begin()
Definition: qmap.h:633
iterator end()
Definition: qmap.h:637
iterator upperBound(const Key &key)
Definition: qmap.h:705
const_iterator constBegin() const
Definition: qmap.h:635
size_type size() const
Definition: qmap.h:302
key_iterator keyBegin() const
Definition: qmap.h:641
Key key(const T &value, const Key &defaultKey=Key()) const
Definition: qmap.h:384
const_iterator constEnd() const
Definition: qmap.h:639
key_iterator keyEnd() const
Definition: qmap.h:642
iterator insert(const Key &key, const T &value)
Definition: qmap.h:1453
The QString class provides a Unicode character string.
Definition: qstring.h:388
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:5092
qDeleteAll(list.begin(), list.end())
std::ostream & cout()
typename C::const_iterator const_iterator
QTextStream & endl(QTextStream &stream)
#define QString()
Definition: parse-defines.h:51
EGLOutputLayerEXT EGLint EGLAttrib value
GLenum GLsizei GLsizei GLint * values
[16]
GLuint GLuint end
GLbitfield GLuint64 timeout
[4]
GLuint name
QStringList keys
QMap< QString, int > map
[0]
QMap< QString, int >::const_iterator upperBound
QMap< QString, int >::const_iterator i
[7]
int num1
[2]
QStringList::Iterator it