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
src_corelib_tools_qhash.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QHash>
5#include <QHashIterator>
6#include <iostream>
7#include <QDate>
8
9#include <utility>
10
11using namespace std;
12
14{
15 //! [0]
16 QHash<QString, int> hash;
17 //! [0]
18
19 {
20 //! [1]
21 hash["one"] = 1;
22 hash["three"] = 3;
23 hash["seven"] = 7;
24 //! [1]
25 }
26
27 {
28 //! [2]
29 hash.insert("twelve", 12);
30 //! [2]
31 }
32
33 {
34 //! [3]
35 int num1 = hash["thirteen"];
36 int num2 = hash.value("thirteen");
37 //! [3]
38 }
39
40 {
41 //! [4]
42 int timeout = 30;
43 if (hash.contains("TIMEOUT"))
44 timeout = hash.value("TIMEOUT");
45 //! [4]
46 }
47
48 {
49 //! [5]
50 int timeout = hash.value("TIMEOUT", 30);
51 //! [5]
52 }
53
54 {
55 //! [7]
56 QHashIterator<QString, int> i(hash);
57 while (i.hasNext()) {
58 i.next();
59 cout << qPrintable(i.key()) << ": " << i.value() << endl;
60 }
61 //! [7]
62 }
63
64 {
65 //! [8]
66 for (const auto &[key, value] : std::as_const(hash).asKeyValueRange())
67 cout << qPrintable(key) << ": " << value << endl;
68 //! [8]
69 }
70
71 {
72 //! [qhash-iterator-stl-style]
73 for (auto it = hash.cbegin(); it != hash.cend(); ++it)
74 cout << qPrintable(it.key()) << ": " << it.value() << endl;
75 //! [qhash-iterator-stl-style]
76 }
77
78 {
79 //! [qhash-iterator-modify-values]
80 for (auto it = hash.begin(); it != hash.end(); ++it)
81 it.value() += 1;
82 //! [qhash-iterator-modify-values]
83 }
84
85 {
86 //! [9]
87 hash.insert("plenty", 100);
88 hash.insert("plenty", 2000);
89 // hash.value("plenty") == 2000
90 //! [9]
91 }
92
93 {
94 //! [12]
95 QHash<QString, int> hash;
96 //...
97 for (int value : std::as_const(hash))
98 cout << value << endl;
99 //! [12]
100 }
101}
102
103#if __has_include(<QWidget>)
104#include <QWidget>
105
106void widget_example(QWidget *okButton)
107{
108 //! [6]
109 // WRONG
110 QHash<int, QWidget *> hash;
111 //...
112 for (int i = 0; i < 1000; ++i) {
113 if (hash[i] == okButton)
114 cout << "Found button at index " << i << endl;
115 }
116 //! [6]
117}
118#endif
119
120//! [13]
121#ifndef EMPLOYEE_H
122#define EMPLOYEE_H
123
124class Employee
125{
126public:
128 Employee(const QString &name, QDate dateOfBirth);
129 QString name() const { return myName; }
130 QDate dateOfBirth() const { return myDateOfBirth; }
131 //...
132
133private:
134 QString myName;
135 QDate myDateOfBirth;
136};
137
138inline bool operator==(const Employee &e1, const Employee &e2)
139{
140 return e1.name() == e2.name()
141 && e1.dateOfBirth() == e2.dateOfBirth();
142}
143
144inline size_t qHash(const Employee &key, size_t seed)
145{
146 return qHashMulti(seed, key.name(), key.dateOfBirth());
147}
148
149#endif // EMPLOYEE_H
150//! [13]
151
152
153void wrap()
154{
155 {
156 QString keys[1];
157 int values[1];
158
159 //! [14]
160 QHash<QString, int> hash;
161 hash.reserve(20000);
162 for (int i = 0; i < 20000; ++i)
163 hash.insert(keys[i], values[i]);
164 //! [14]
165 }
166
167 {
168 QObject *obj;
169
170 //! [15]
171 QHash<QObject *, int> objectHash;
172 //...
173 QHash<QObject *, int>::iterator i = objectHash.find(obj);
174 while (i != objectHash.end() && i.key() == obj) {
175 if (i.value() == 0) {
176 i = objectHash.erase(i);
177 } else {
178 ++i;
179 }
180 }
181 //! [15]
182 }
183
184 {
185 QObject *obj;
186
187 //! [15multihash]
188 QMultiHash<QObject *, int> objectHash;
189 //...
190 QMultiHash<QObject *, int>::iterator i = objectHash.find(obj);
191 while (i != objectHash.end() && i.key() == obj) {
192 if (i.value() == 0) {
193 i = objectHash.erase(i);
194 } else {
195 ++i;
196 }
197 }
198 //! [15multihash]
199 }
200
201 {
202 //! [16]
203 QHash<QString, int> hash;
204 //...
205 QHash<QString, int>::const_iterator i = hash.find("HDR");
206 while (i != hash.end() && i.key() == "HDR") {
207 cout << i.value() << endl;
208 ++i;
209 }
210 //! [16]
211 }
212
213 {
214 //! [17]
215 QHash<QString, int> hash;
216 hash.insert("January", 1);
217 hash.insert("February", 2);
218 //...
219 hash.insert("December", 12);
220
221 for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i)
222 cout << qPrintable(i.key()) << ": " << i.value() << endl;
223 //! [17]
224 }
225
226 {
227 QHash<QString, int> hash;
228
229 //! [18]
230 for (auto i = hash.begin(), end = hash.end(); i != end; ++i)
231 i.value() += 2;
232 //! [18]
233 }
234
235 {
236 QHash<QString, int> hash;
237
238 //! [21]
239 erase_if(hash, [](const QHash<QString, int>::iterator it) { return it.value() > 10; });
240 //! [21]
241 }
242
243 {
244 QHash<QString, QString> hash;
245 auto i = hash.begin();
246
247 //! [22]
248 if (i.key() == "Hello")
249 i.value() = "Bonjour";
250 //! [22]
251 }
252
253 {
254 //! [23]
255 QHash<QString, int> hash;
256 hash.insert("January", 1);
257 hash.insert("February", 2);
258 //...
259 hash.insert("December", 12);
260
261 for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i)
262 cout << qPrintable(i.key()) << ": " << i.value() << endl;
263 //! [23]
264 }
265
266 {
267 //! [24]
268 QMultiHash<QString, int> hash1, hash2, hash3;
269
270 hash1.insert("plenty", 100);
271 hash1.insert("plenty", 2000);
272 // hash1.size() == 2
273
274 hash2.insert("plenty", 5000);
275 // hash2.size() == 1
276
277 hash3 = hash1 + hash2;
278 // hash3.size() == 3
279 //! [24]
280 }
281
282 {
283 QMultiHash<QString, int> hash;
284
285 //! [25]
286 QList<int> values = hash.values("plenty");
287 for (auto i : std::as_const(values))
288 cout << i << endl;
289 //! [25]
290 }
291
292 {
293 QMultiHash<QString, int> hash;
294
295 //! [26]
296 auto i = hash.constFind("plenty");
297 while (i != hash.cend() && i.key() == "plenty") {
298 cout << i.value() << endl;
299 ++i;
300 }
301 //! [26]
302 }
303
304 {
305 QMultiHash<int, QString> hash;
306
307 //! [27]
308 for (auto it = hash.cbegin(), end = hash.cend(); it != end; ++it) {
309 cout << "The key: " << it.key() << endl;
310 cout << "The value: " << qPrintable(it.value()) << endl;
311 cout << "Also the value: " << qPrintable(*it) << endl;
312 }
313 //! [27]
314 }
315
316 {
317 QHash<int, int> hash;
318 QHash<QObject *, int> hash2;
319 auto isPrimeNumber = [](int num) { return true; };
320
321 //! [28]
322 // Inefficient, keys() is expensive
323 QList<int> keys = hash.keys();
324 int numPrimes = std::count_if(keys.cbegin(), keys.cend(), isPrimeNumber);
325 qDeleteAll(hash2.keys());
326
327 // Efficient, no memory allocation needed
328 int primeNums = std::count_if(hash.keyBegin(), hash.keyEnd(), isPrimeNumber);
329 qDeleteAll(hash2.keyBegin(), hash2.keyEnd());
330 //! [28]
331 }
332}
333
334//! [qhashbits]
335inline size_t qHash(const std::vector<int> &key, size_t seed = 0)
336{
337 if (key.empty())
338 return seed;
339 else
340 return qHashBits(&key.front(), key.size() * sizeof(int), seed);
341}
342//! [qhashbits]
343
345{
346 //! [qhashrange]
347 inline size_t qHash(const std::vector<int> &key, size_t seed = 0)
348 {
349 return qHashRange(key.begin(), key.end(), seed);
350 }
351 //! [qhashrange]
352}
353
355{
356 //! [qhashrangecommutative]
357 inline size_t qHash(const std::unordered_set<int> &key, size_t seed = 0)
358 {
359 return qHashRangeCommutative(key.begin(), key.end(), seed);
360 }
361 //! [qhashrangecommutative]
362}
363
364#if TEXT
365//! [30]
366{0, 1, 2}
367//! [30]
368
369//! [31]
370{1, 2, 0}
371//! [31]
372#endif
373
374struct K {};
375
377{
378 {
379 //! [32]
380 size_t qHash(K key, size_t seed);
381 size_t qHash(const K &key, size_t seed);
382
383 size_t qHash(K key); // deprecated, do not use
384 size_t qHash(const K &key); // deprecated, do not use
385 //! [32]
386 }
387
388 {
389 //! [34]
390 QHash<QString, int> hash;
391 hash.insert("January", 1);
392 hash.insert("February", 2);
393 // ...
394 hash.insert("December", 12);
395
396 for (auto [key, value] : hash.asKeyValueRange()) {
397 cout << qPrintable(key) << ": " << value << endl;
398 --value; // convert to JS month indexing
399 }
400 //! [34]
401 }
402
403 {
404 //! [35]
405 QMultiHash<QString, int> hash;
406 hash.insert("January", 1);
407 hash.insert("February", 2);
408 // ...
409 hash.insert("December", 12);
410
411 for (auto [key, value] : hash.asKeyValueRange()) {
412 cout << qPrintable(key) << ": " << value << endl;
413 --value; // convert to JS month indexing
414 }
415 //! [35]
416 }
417}
418
419//! [33]
420namespace std
421{
422 template <> struct hash<K>
423 {
424 // seed is optional
425 size_t operator()(const K &key, size_t seed = 0) const;
426 };
427}
428//! [33]
QString name() const
Employee(const QString &name, QDate dateOfBirth)
QDate dateOfBirth() const
void wrap()
[23]
size_t qHash(const std::vector< int > &key, size_t seed=0)
[qhashrange]
size_t qHash(const std::unordered_set< int > &key, size_t seed=0)
[qhashrangecommutative]
#define __has_include(x)
bool examples()
[3]
size_t qHash(const std::vector< int > &key, size_t seed=0)
[qhashbits]
bool operator==(const Employee &e1, const Employee &e2)
void snippets()
size_t qHash(const Employee &key, size_t seed)
size_t operator()(const K &key, size_t seed=0) const