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_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
6QMultiMap<QString, int> multimap;
8
9
11multimap.insert("a", 1);
12multimap.insert("b", 3);
13multimap.insert("c", 7);
14multimap.insert("c", -5);
16
17
19int num2 = multimap.value("a"); // 1
20int num3 = multimap.value("thirteen"); // not found; 0
21int num3 = 0;
22auto it = multimap.constFind("b");
23if (it != multimap.cend()) {
24 num3 = it.value();
25}
27
28
30int timeout = 30;
31if (multimap.contains("TIMEOUT"))
32 timeout = multimap.value("TIMEOUT");
33
34// better:
35auto it = multimap.find("TIMEOUT");
36if (it != multimap.end())
37 timeout = it.value();
39
40
42int timeout = multimap.value("TIMEOUT", 30);
44
45
47QMultiMapIterator<QString, int> i(multimap);
48while (i.hasNext()) {
49 i.next();
50 cout << qPrintable(i.key()) << ": " << i.value() << endl;
51}
53
54
56for (auto i = multimap.cbegin(), end = multimap.cend(); i != end; ++i)
57 cout << qPrintable(i.key()) << ": " << i.value() << endl;
59
60
62multimap.insert("plenty", 100);
63multimap.insert("plenty", 2000);
64// multimap.size() == 2
66
67
69QList<int> values = multimap.values("plenty");
70for (auto i : std::as_const(values))
71 cout << i << endl;
73
74
76auto i = multimap.find("plenty");
77while (i != map.end() && i.key() == "plenty") {
78 cout << i.value() << endl;
79 ++i;
80}
81
82// better:
83auto [i, end] = multimap.equal_range("plenty");
84while (i != end) {
85 cout << i.value() << endl;
86 ++i;
87}
89
90
92QMap<QString, int> multimap;
93...
94for (int value : std::as_const(multimap))
95 cout << value << endl;
97
98
100#ifndef EMPLOYEE_H
101#define EMPLOYEE_H
102
103class Employee
104{
105public:
106 Employee() {}
107 Employee(const QString &name, QDate dateOfBirth);
108 ...
109
110private:
111 QString myName;
112 QDate myDateOfBirth;
113};
114
115inline bool operator<(const Employee &e1, const Employee &e2)
116{
117 if (e1.name() != e2.name())
118 return e1.name() < e2.name();
119 return e1.dateOfBirth() < e2.dateOfBirth();
120}
121
122#endif // EMPLOYEE_H
124
125
127QMultiMap<int, QString> multimap;
128multimap.insert(1, "one");
129multimap.insert(5, "five");
130multimap.insert(5, "five (2)");
131multimap.insert(10, "ten");
132
133multimap.lowerBound(0); // returns iterator to (1, "one")
134multimap.lowerBound(1); // returns iterator to (1, "one")
135multimap.lowerBound(2); // returns iterator to (5, "five")
136multimap.lowerBound(5); // returns iterator to (5, "five")
137multimap.lowerBound(6); // returns iterator to (10, "ten")
138multimap.lowerBound(10); // returns iterator to (10, "ten")
139multimap.lowerBound(999); // returns end()
141
142
144QMap<QString, int> multimap;
145...
146QMap<QString, int>::const_iterator i = multimap.lowerBound("HDR");
147QMap<QString, int>::const_iterator upperBound = multimap.upperBound("HDR");
148while (i != upperBound) {
149 cout << i.value() << endl;
150 ++i;
151}
153
154
156QMultiMap<int, QString> multimap;
157multimap.insert(1, "one");
158multimap.insert(5, "five");
159multimap.insert(5, "five (2)");
160multimap.insert(10, "ten");
161
162multimap.upperBound(0); // returns iterator to (1, "one")
163multimap.upperBound(1); // returns iterator to (5, "five")
164multimap.upperBound(2); // returns iterator to (5, "five")
165multimap.lowerBound(5); // returns iterator to (5, "five (2)")
166multimap.lowerBound(6); // returns iterator to (10, "ten")
167multimap.upperBound(10); // returns end()
168multimap.upperBound(999); // returns end()
170
172for (auto it = multimap.begin(), end = multimap.end(); i != end; ++i)
173 i.value() += 2;
175
176void erase()
177{
178QMultiMap<QString, int> multimap;
181while (i != multimap.cend()) {
182 if (i.value() > 10)
183 i = multimap.erase(i);
184 else
185 ++i;
186}
189erase_if(multimap, [](const QMultiMap<QString, int>::iterator it) { return it.value() > 10; });
191}
192
193
195if (i.key() == "Hello")
196 i.value() = "Bonjour";
198
199
201QMultiMap<QString, int> multi;
202multimap.insert("January", 1);
203multimap.insert("February", 2);
204...
205multimap.insert("December", 12);
206
207for (auto i = multimap.cbegin(), end = multimap.cend(); i != end; ++i)
208 cout << qPrintable(i.key()) << ": " << i.value() << endl;
210
211
213QMultiMap<QString, int> map1, map2, map3;
214
215map1.insert("plenty", 100);
216map1.insert("plenty", 2000);
217// map1.size() == 2
218
219map2.insert("plenty", 5000);
220// map2.size() == 1
221
222map3 = map1 + map2;
223// map3.size() == 3
225
227for (auto it = multimap.cbegin(), end = multimap.cend(); it != end; ++it) {
228 cout << "The key: " << it.key() << endl
229 cout << "The value: " << qPrintable(it.value()) << endl;
230 cout << "Also the value: " << qPrintable(*it) << endl;
231}
233
235// Inefficient, keys() is expensive
236QList<int> keys = multimap.keys();
237int numPrimes = std::count_if(multimap.cbegin(), multimap.cend(), isPrimeNumber);
238qDeleteAll(multimap2.keys());
239
240// Efficient, no memory allocation needed
241int numPrimes = std::count_if(multimap.keyBegin(), multimap.keyEnd(), isPrimeNumber);
242qDeleteAll(multimap2.keyBegin(), multimap2.keyEnd());
244
246QMultiMap<QString, int> map;
247map.insert("January", 1);
248map.insert("February", 2);
249// ...
250map.insert("December", 12);
251
252for (auto [key, value] : map.asKeyValueRange()) {
253 cout << qPrintable(key) << ": " << value << endl;
254 --value; // convert to JS month indexing
255}
QString name() const
[5] //! [6]
Definition employee.h:49
\inmodule QtCore \reentrant
Definition qdatetime.h:29
iterator insert(const Key &key, const T &value)
Definition qmap.h:688
iterator end()
Definition qmap.h:602
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QMap< QString, QString > map
[6]
qDeleteAll(list.begin(), list.end())
qsizetype erase_if(QByteArray &ba, Predicate pred)
Definition qbytearray.h:788
qsizetype erase(QByteArray &ba, const T &t)
Definition qbytearray.h:782
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLenum GLsizei GLsizei GLint * values
[15]
GLuint64 key
GLuint GLuint end
GLbitfield GLuint64 timeout
[4]
GLuint name
static bool operator<(const QSettingsIniKey &k1, const QSettingsIniKey &k2)
#define qPrintable(string)
Definition qstring.h:1531
QStringList keys
QMultiMap< QString, int > multimap
[0]