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_qmultimap.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
2// Copyright (C) 2016 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
4
5#include <QMultiMap>
6#include <QMultiMapIterator>
7#include <iostream>
8#include <QDate>
9
10using namespace std;
11
13{
14 //! [0]
15 QMultiMap<QString, int> multimap;
16 //! [0]
17
18 {
19 //! [2]
20 multimap.insert("a", 1);
21 multimap.insert("b", 3);
22 multimap.insert("c", 7);
23 multimap.insert("c", -5);
24 //! [2]
25 }
26
27 {
28 //! [3]
29 int num2 = multimap.value("a"); // 1
30 int num3 = multimap.value("thirteen"); // not found; 0
31 auto it = multimap.constFind("b");
32 if (it != multimap.cend()) {
33 num3 = it.value();
34 }
35 //! [3]
36 }
37
38 {
39 //! [4]
40 int timeout = 30;
41 if (multimap.contains("TIMEOUT"))
42 timeout = multimap.value("TIMEOUT");
43
44 // better:
45 auto it = multimap.find("TIMEOUT");
46 if (it != multimap.end())
47 timeout = it.value();
48 //! [4]
49 }
50
51 {
52 //! [5]
53 int timeout = multimap.value("TIMEOUT", 30);
54 //! [5]
55 }
56
57 {
58 //! [7]
59 QMultiMapIterator<QString, int> i(multimap);
60 while (i.hasNext()) {
61 i.next();
62 cout << qPrintable(i.key()) << ": " << i.value() << endl;
63 }
64 //! [7]
65 }
66
67 {
68 //! [8]
69 for (auto i = multimap.cbegin(), end = multimap.cend(); i != end; ++i)
70 cout << qPrintable(i.key()) << ": " << i.value() << endl;
71 //! [8]
72 }
73
74 {
75 //! [9]
76 multimap.insert("plenty", 100);
77 multimap.insert("plenty", 2000);
78 // multimap.size() == 2
79 //! [9]
80 }
81
82 {
83 //! [10]
84 QList<int> values = multimap.values("plenty");
85 for (auto i : std::as_const(values))
86 cout << i << endl;
87 //! [10]
88 }
89
90 {
91 //! [11]
92 auto i = multimap.find("plenty");
93 while (i != multimap.end() && i.key() == "plenty") {
94 cout << i.value() << endl;
95 ++i;
96 }
97
98 //! [11]
99 }
100
101 {
102 //! [11_better]
103 // better:
104 auto [i, end] = multimap.equal_range("plenty");
105 while (i != end) {
106 cout << i.value() << endl;
107 ++i;
108 }
109 //! [11_better]
110 }
111
112 {
113 //! [12]
114 QMap<QString, int> multimap;
115 //...
116 for (int value : std::as_const(multimap))
117 cout << value << endl;
118 //! [12]
119 }
120}
121
122//! [13]
123#ifndef EMPLOYEE_H
124#define EMPLOYEE_H
125
126class Employee
127{
128public:
130 Employee(const QString &name, QDate dateOfBirth);
131 QString name() const { return myName; }
132 QDate dateOfBirth() const { return myDateOfBirth; }
133 //...
134
135private:
136 QString myName;
137 QDate myDateOfBirth;
138};
139
140inline bool operator<(const Employee &e1, const Employee &e2)
141{
142 if (e1.name() != e2.name())
143 return e1.name() < e2.name();
144 return e1.dateOfBirth() < e2.dateOfBirth();
145}
146
147#endif // EMPLOYEE_H
148//! [13]
149
150
151void erase()
152{
153 {
154 //! [15]
155 QMultiMap<int, QString> multimap;
156 multimap.insert(1, "one");
157 multimap.insert(5, "five");
158 multimap.insert(5, "five (2)");
159 multimap.insert(10, "ten");
160
161 multimap.lowerBound(0); // returns iterator to (1, "one")
162 multimap.lowerBound(1); // returns iterator to (1, "one")
163 multimap.lowerBound(2); // returns iterator to (5, "five")
164 multimap.lowerBound(5); // returns iterator to (5, "five")
165 multimap.lowerBound(6); // returns iterator to (10, "ten")
166 multimap.lowerBound(10); // returns iterator to (10, "ten")
167 multimap.lowerBound(999); // returns end()
168 //! [15]
169 }
170
171 {
172 //! [16]
173 QMap<QString, int> multimap;
174 //...
175 QMap<QString, int>::const_iterator i = multimap.lowerBound("HDR");
176 QMap<QString, int>::const_iterator upperBound = multimap.upperBound("HDR");
177 while (i != upperBound) {
178 cout << i.value() << endl;
179 ++i;
180 }
181 //! [16]
182 }
183
184 {
185 //! [17]
186 QMultiMap<int, QString> multimap;
187 multimap.insert(1, "one");
188 multimap.insert(5, "five");
189 multimap.insert(5, "five (2)");
190 multimap.insert(10, "ten");
191
192 multimap.upperBound(0); // returns iterator to (1, "one")
193 multimap.upperBound(1); // returns iterator to (5, "five")
194 multimap.upperBound(2); // returns iterator to (5, "five")
195 multimap.lowerBound(5); // returns iterator to (5, "five (2)")
196 multimap.lowerBound(6); // returns iterator to (10, "ten")
197 multimap.upperBound(10); // returns end()
198 multimap.upperBound(999); // returns end()
199 //! [17]
200 }
201
202 {
203 QMultiMap<QString, int> multimap;
204
205 //! [19]
206 for (auto i = multimap.begin(), end = multimap.end(); i != end; ++i)
207 i.value() += 2;
208 //! [19]
209 }
210
211 {
212 QMultiMap<QString, int> multimap;
213
214 //! [20]
215 QMultiMap<QString, int>::const_iterator i = multimap.cbegin();
216 while (i != multimap.cend()) {
217 if (i.value() > 10)
218 i = multimap.erase(i);
219 else
220 ++i;
221 }
222 //! [20]
223 }
224
225 {
226 QMultiMap<QString, int> multimap;
227
228 //! [21]
229 erase_if(multimap, [](const QMultiMap<QString, int>::iterator it) { return it.value() > 10; });
230 //! [21]
231 }
232
233 {
234 auto i = QMultiMap<QString, QString>::iterator();
235
236 //! [23]
237 if (i.key() == "Hello")
238 i.value() = "Bonjour";
239 //! [23]
240 }
241
242 {
243 //! [24]
244 QMultiMap<QString, int> multimap;
245 multimap.insert("January", 1);
246 multimap.insert("February", 2);
247 //...
248 multimap.insert("December", 12);
249
250 for (auto i = multimap.cbegin(), end = multimap.cend(); i != end; ++i)
251 cout << qPrintable(i.key()) << ": " << i.value() << endl;
252 //! [24]
253 }
254
255 {
256 //! [25]
257 QMultiMap<QString, int> map1, map2, map3;
258
259 map1.insert("plenty", 100);
260 map1.insert("plenty", 2000);
261 // map1.size() == 2
262
263 map2.insert("plenty", 5000);
264 // map2.size() == 1
265
266 map3 = map1 + map2;
267 // map3.size() == 3
268 //! [25]
269 }
270
271 {
272 QMultiMap<int, QString> multimap;
273
274 //! [keyiterator1]
275 for (auto it = multimap.cbegin(), end = multimap.cend(); it != end; ++it) {
276 cout << "The key: " << it.key() << endl;
277 cout << "The value: " << qPrintable(it.value()) << endl;
278 cout << "Also the value: " << qPrintable(*it) << endl;
279 }
280 //! [keyiterator1]
281 }
282
283 {
284 QMultiMap<int, int> multimap;
285 QMultiMap<QObject *, int> multimap2;
286 auto isPrimeNumber = [](int num) { return true; };
287
288 //! [keyiterator2]
289 // Inefficient, keys() is expensive
290 QList<int> keys = multimap.keys();
291 int numPrimes = std::count_if(multimap.cbegin(), multimap.cend(), isPrimeNumber);
292 qDeleteAll(multimap2.keys());
293
294 // Efficient, no memory allocation needed
295 int primeNums = std::count_if(multimap.keyBegin(), multimap.keyEnd(), isPrimeNumber);
296 qDeleteAll(multimap2.keyBegin(), multimap2.keyEnd());
297 //! [keyiterator2]
298 }
299
300 {
301 //! [26]
302 QMultiMap<QString, int> map;
303 map.insert("January", 1);
304 map.insert("February", 2);
305 // ...
306 map.insert("December", 12);
307
308 for (auto [key, value] : map.asKeyValueRange()) {
309 cout << qPrintable(key) << ": " << value << endl;
310 --value; // convert to JS month indexing
311 }
312 //! [26]
313 }
314}
QString name() const
Employee(const QString &name, QDate dateOfBirth)
QDate dateOfBirth() const
bool examples()
[3]
bool operator<(const Employee &e1, const Employee &e2)
void erase()
[13]