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
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
5QHash<QString, int> hash;
7
8
10hash["one"] = 1;
11hash["three"] = 3;
12hash["seven"] = 7;
14
15
17hash.insert("twelve", 12);
19
20
22int num1 = hash["thirteen"];
23int num2 = hash.value("thirteen");
25
26
28int timeout = 30;
29if (hash.contains("TIMEOUT"))
30 timeout = hash.value("TIMEOUT");
32
33
35int timeout = hash.value("TIMEOUT", 30);
37
38
40// WRONG
41QHash<int, QWidget *> hash;
42...
43for (int i = 0; i < 1000; ++i) {
44 if (hash[i] == okButton)
45 cout << "Found button at index " << i << endl;
46}
48
49
51QHashIterator<QString, int> i(hash);
52while (i.hasNext()) {
53 i.next();
54 cout << qPrintable(i.key()) << ": " << i.value() << endl;
55}
57
58
60for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i)
61 cout << qPrintable(i.key()) << ": " << i.value() << endl;
63
64
66hash.insert("plenty", 100);
67hash.insert("plenty", 2000);
68// hash.value("plenty") == 2000
70
71
73QHash<QString, int> hash;
74...
75for (int value : std::as_const(hash))
76 cout << value << endl;
78
79
81#ifndef EMPLOYEE_H
82#define EMPLOYEE_H
83
84class Employee
85{
86public:
87 Employee() {}
88 Employee(const QString &name, QDate dateOfBirth);
89 ...
90
91private:
92 QString myName;
93 QDate myDateOfBirth;
94};
95
96inline bool operator==(const Employee &e1, const Employee &e2)
97{
98 return e1.name() == e2.name()
99 && e1.dateOfBirth() == e2.dateOfBirth();
100}
101
102inline size_t qHash(const Employee &key, size_t seed)
103{
104 return qHashMulti(seed, key.name(), key.dateOfBirth());
105}
106
107#endif // EMPLOYEE_H
109
110
112QHash<QString, int> hash;
113hash.reserve(20000);
114for (int i = 0; i < 20000; ++i)
115 hash.insert(keys[i], values[i]);
117
118
120QHash<QObject *, int> objectHash;
121...
122QHash<QObject *, int>::iterator i = objectHash.find(obj);
123while (i != objectHash.end() && i.key() == obj) {
124 if (i.value() == 0) {
125 i = objectHash.erase(i);
126 } else {
127 ++i;
128 }
129}
131
132
134QHash<QString, int> hash;
135...
136QHash<QString, int>::const_iterator i = hash.find("HDR");
137while (i != hash.end() && i.key() == "HDR") {
138 cout << i.value() << endl;
139 ++i;
140}
142
143
145QHash<QString, int> hash;
146hash.insert("January", 1);
147hash.insert("February", 2);
148...
149hash.insert("December", 12);
150
151for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i)
152 cout << qPrintable(key()) << ": " << i.value() << endl;
154
155
157for (auto i = hash.begin(), end = hash.end(); i != end; ++i)
158 i.value() += 2;
160
162erase_if(hash, [](const QHash<QString, int>::iterator it) { return it.value() > 10; });
164}
165
167if (i.key() == "Hello")
168 i.value() = "Bonjour";
170
171
173QHash<QString, int> hash;
174hash.insert("January", 1);
175hash.insert("February", 2);
176...
177hash.insert("December", 12);
178
179for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i)
180 cout << qPrintable(i.key()) << ": " << i.value() << endl;
182
183
185QMultiHash<QString, int> hash1, hash2, hash3;
186
187hash1.insert("plenty", 100);
188hash1.insert("plenty", 2000);
189// hash1.size() == 2
190
191hash2.insert("plenty", 5000);
192// hash2.size() == 1
193
194hash3 = hash1 + hash2;
195// hash3.size() == 3
197
198
200QList<int> values = hash.values("plenty");
201for (auto i : std::as_const(values))
202 cout << i << endl;
204
205
207auto i = hash.constFind("plenty");
208while (i != hash.cend() && i.key() == "plenty") {
209 cout << i.value() << endl;
210 ++i;
211}
213
215for (auto it = hash.cbegin(), end = hash.cend(); it != end; ++it) {
216 cout << "The key: " << it.key() << endl;
217 cout << "The value: " << qPrintable(it.value()) << endl;
218 cout << "Also the value: " << qPrintable(*it) << endl;
219}
221
223// Inefficient, keys() is expensive
224QList<int> keys = hash.keys();
225int numPrimes = std::count_if(keys.cbegin(), keys.cend(), isPrimeNumber);
226qDeleteAll(hash2.keys());
227
228// Efficient, no memory allocation needed
229int numPrimes = std::count_if(hash.keyBegin(), hash.keyEnd(), isPrimeNumber);
230qDeleteAll(hash2.keyBegin(), hash2.keyEnd());
232
234inline size_t qHash(const std::vector<int> &key, size_t seed = 0)
235{
236 if (key.empty())
237 return seed;
238 else
239 return qHashBits(&key.front(), key.size() * sizeof(int), seed);
240}
242
244inline size_t qHash(const std::vector<int> &key, size_t seed = 0)
245{
246 return qHashRange(key.begin(), key.end(), seed);
247}
249
251inline size_t qHash(const std::unordered_set<int> &key, size_t seed = 0)
252{
253 return qHashRangeCommutative(key.begin(), key.end(), seed);
254}
256
258{0, 1, 2}
260
262{1, 2, 0}
264
266size_t qHash(K key, size_t seed);
267size_t qHash(const K &key, size_t seed);
268
269size_t qHash(K key); // deprecated, do not use
270size_t qHash(const K &key); // deprecated, do not use
272
274namespace std {
275template <> struct hash<K>
276{
277 // seed is optional
278 size_t operator()(const K &key, size_t seed = 0) const;
279};
280}
282
284QHash<QString, int> hash;
285hash.insert("January", 1);
286hash.insert("February", 2);
287// ...
288hash.insert("December", 12);
289
290for (auto [key, value] : hash.asKeyValueRange()) {
291 cout << qPrintable(key) << ": " << value << endl;
292 --value; // convert to JS month indexing
293}
295
297QMultiHash<QString, int> hash;
298hash.insert("January", 1);
299hash.insert("February", 2);
300// ...
301hash.insert("December", 12);
302
303for (auto [key, value] : hash.asKeyValueRange()) {
304 cout << qPrintable(key) << ": " << value << endl;
305 --value; // convert to JS month indexing
306}
QString name() const
[5] //! [6]
Definition employee.h:49
\inmodule QtCore \reentrant
Definition qdatetime.h:29
\inmodule QtCore
Definition qhash.h:1103
key_iterator keyEnd() const noexcept
Definition qhash.h:1221
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1212
const_iterator cbegin() const noexcept
Definition qhash.h:1214
const_iterator constFind(const Key &key) const noexcept
Definition qhash.h:1299
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition qhash.h:1291
QList< T > values() const
Returns a list containing all the values in the hash, in an arbitrary order.
Definition qhash.h:1098
void reserve(qsizetype size)
Ensures that the QHash's internal hash table has space to store at least size items without having to...
Definition qhash.h:931
QList< Key > keys() const
Returns a list containing all the keys in the hash, in an arbitrary order.
Definition qhash.h:1086
bool contains(const Key &key) const noexcept
Returns true if the hash contains an item with the key; otherwise returns false.
Definition qhash.h:1007
key_iterator keyBegin() const noexcept
Definition qhash.h:1220
T value(const Key &key) const noexcept
Definition qhash.h:1054
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1216
const_iterator cend() const noexcept
Definition qhash.h:1218
QHash() noexcept=default
Constructs an empty hash.
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1303
\inmodule QtCore
Definition qobject.h:103
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QHash< int, QWidget * > hash
[35multi]
qDeleteAll(list.begin(), list.end())
QSet< QString >::iterator it
qsizetype erase_if(QByteArray &ba, Predicate pred)
Definition qbytearray.h:788
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
size_t qHash(const QFileSystemWatcherPathKey &key, size_t seed=0)
size_t qHashBits(const void *p, size_t size, size_t seed) noexcept
Definition qhash.cpp:1089
size_t qHashRange(InputIterator first, InputIterator last, size_t seed=0) noexcept(noexcept(qHash(*first)))
size_t qHashRangeCommutative(InputIterator first, InputIterator last, size_t seed=0) noexcept(noexcept(qHash(*first)))
constexpr QtPrivate::QHashMultiReturnType< T... > qHashMulti(size_t seed, const T &... args) noexcept(std::conjunction_v< QtPrivate::QNothrowHashable< T >... >)
GLenum GLsizei GLsizei GLint * values
[15]
GLuint64 key
GLuint GLuint end
GLbitfield GLuint64 timeout
[4]
GLuint name
GLhandleARB obj
[2]
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
#define qPrintable(string)
Definition qstring.h:1531
QStringList keys
QHash< QString, int > hash
[0]