Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
src_corelib_tools_qmap.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4
#
include
<
QMap
>
5
#
include
<
QMapIterator
>
6
#
include
<
iostream
>
7
#
include
<
QDate
>
8
9
using
namespace
std
;
10
11
void
examples
()
12
{
13
//! [0]
14
QMap<QString,
int
> map;
15
//! [0]
16
17
{
18
//! [1]
19
map[
"one"
] = 1;
20
map[
"three"
] = 3;
21
map[
"seven"
] = 7;
22
//! [1]
23
}
24
25
{
26
//! [2]
27
map.insert(
"twelve"
, 12);
28
//! [2]
29
}
30
31
{
32
//! [3]
33
int
num1 = map[
"thirteen"
];
34
int
num2 = map.value(
"thirteen"
);
35
//! [3]
36
}
37
38
{
39
//! [4]
40
int
timeout = 30;
41
if
(map.contains(
"TIMEOUT"
))
42
timeout = map.value(
"TIMEOUT"
);
43
//! [4]
44
}
45
46
{
47
//! [5]
48
int
timeout = map.value(
"TIMEOUT"
, 30);
49
//! [5]
50
}
51
52
{
53
//! [7]
54
QMapIterator<QString,
int
> i(map);
55
while
(i.hasNext()) {
56
i.next();
57
cout << qPrintable(i.key()) <<
": "
<< i.value() << endl;
58
}
59
//! [7]
60
}
61
62
{
63
//! [8]
64
for
(
auto
i = map.cbegin(), end = map.cend(); i != end; ++i)
65
cout << qPrintable(i.key()) <<
": "
<< i.value() << endl;
66
//! [8]
67
}
68
69
{
70
//! [9]
71
map.insert(
"plenty"
, 100);
72
map.insert(
"plenty"
, 2000);
73
// map.value("plenty") == 2000
74
//! [9]
75
}
76
77
{
78
//! [12]
79
QMap<QString,
int
> map;
80
//...
81
for
(
int
value : std::as_const(map))
82
cout << value << endl;
83
//! [12]
84
}
85
}
86
87
#
if
__has_include
(
<
QWidget
>
)
88
#
include
<
QWidget
>
89
90
void
widget_example(QWidget *okButton)
91
{
92
//! [6]
93
// WRONG
94
QMap<
int
, QWidget *> map;
95
//...
96
for
(
int
i = 0; i < 1000; ++i) {
97
if
(map[i] == okButton)
98
cout <<
"Found button at index "
<< i << endl;
99
}
100
//! [6]
101
}
102
#
endif
103
104
105
//! [13]
106
#
ifndef
EMPLOYEE_H
107
#
define
EMPLOYEE_H
108
109
class
Employee
110
{
111
public
:
112
Employee
() {}
113
Employee
(
const
QString &name, QDate dateOfBirth);
114
QString
name
()
const
{
return
myName; }
115
QDate
dateOfBirth
()
const
{
return
myDateOfBirth; }
116
//...
117
118
private
:
119
QString myName;
120
QDate myDateOfBirth;
121
};
122
123
inline
bool
operator
<(
const
Employee
&e1,
const
Employee
&e2)
124
{
125
if
(e1.name() != e2.name())
126
return
e1.name() < e2.name();
127
return
e1.dateOfBirth() < e2.dateOfBirth();
128
}
129
130
#
endif
// EMPLOYEE_H
131
//! [13]
132
133
void
erase
()
134
{
135
{
136
//! [17]
137
QMap<
int
, QString> map;
138
map.insert(1,
"one"
);
139
map.insert(5,
"five"
);
140
map.insert(10,
"ten"
);
141
142
map.upperBound(0);
// returns iterator to (1, "one")
143
map.upperBound(1);
// returns iterator to (5, "five")
144
map.upperBound(2);
// returns iterator to (5, "five")
145
map.upperBound(10);
// returns end()
146
map.upperBound(999);
// returns end()
147
//! [17]
148
}
149
150
{
151
//! [18]
152
QMap<QString,
int
> map;
153
map.insert(
"January"
, 1);
154
map.insert(
"February"
, 2);
155
//...
156
map.insert(
"December"
, 12);
157
158
for
(
auto
i = map.cbegin(), end = map.cend(); i != end; ++i)
159
cout << qPrintable(i.key()) <<
": "
<< i.value() << endl;
160
//! [18]
161
}
162
163
{
164
QMap<QString,
int
> map;
165
166
//! [19]
167
for
(
auto
i = map.begin(), end = map.end(); i != end; ++i)
168
i.value() += 2;
169
//! [19]
170
}
171
172
{
173
QMap<QString,
int
> map;
174
175
//! [20]
176
QMap<QString,
int
>::const_iterator i = map.cbegin();
177
while
(i != map.cend()) {
178
if
(i.value() > 10)
179
i = map.erase(i);
180
else
181
++i;
182
}
183
//! [20]
184
}
185
186
{
187
QMap<QString,
int
> map;
188
189
//! [21]
190
erase_if(map, [](
const
QMap<QString,
int
>::iterator it) {
return
it.value() > 10; });
191
//! [21]
192
}
193
194
{
195
QMap<QString, QString> map;
196
auto
i = map.begin();
197
198
//! [23]
199
if
(i.key() ==
"Hello"
)
200
i.value() =
"Bonjour"
;
201
//! [23]
202
}
203
204
{
205
//! [24]
206
QMap<QString,
int
> map;
207
map.insert(
"January"
, 1);
208
map.insert(
"February"
, 2);
209
//...
210
map.insert(
"December"
, 12);
211
212
for
(
auto
i = map.cbegin(), end = map.cend(); i != end; ++i)
213
cout << qPrintable(i.key()) <<
": "
<< i.value() << endl;
214
//! [24]
215
}
216
217
{
218
QMap<
int
, QString> map;
219
220
//! [keyiterator1]
221
for
(QMap<
int
, QString>::const_iterator it = map.cbegin(), end = map.cend(); it != end; ++it) {
222
cout <<
"The key: "
<< it.key() << endl;
223
cout <<
"The value: "
<< qPrintable(it.value()) << endl;
224
cout <<
"Also the value: "
<< qPrintable(*it) << endl;
225
}
226
//! [keyiterator1]
227
}
228
229
{
230
QMap<
int
,
int
> map;
231
QMap<QObject *,
int
> map2;
232
auto
isPrimeNumber = [](
int
num) {
return
true
; };
233
234
//! [keyiterator2]
235
// Inefficient, keys() is expensive
236
QList<
int
> keys = map.keys();
237
int
numPrimes = std::count_if(map.cbegin(), map.cend(), isPrimeNumber);
238
qDeleteAll(map2.keys());
239
240
// Efficient, no memory allocation needed
241
int
primeNums = std::count_if(map.keyBegin(), map.keyEnd(), isPrimeNumber);
242
qDeleteAll(map2.keyBegin(), map2.keyEnd());
243
//! [keyiterator2]
244
}
245
246
{
247
//! [28]
248
QMap<QString,
int
> map;
249
map.insert(
"January"
, 1);
250
map.insert(
"February"
, 2);
251
// ...
252
map.insert(
"December"
, 12);
253
254
for
(
auto
[key, value] : map.asKeyValueRange()) {
255
cout << qPrintable(key) <<
": "
<< value << endl;
256
--value;
// convert to JS month indexing
257
}
258
//! [28]
259
}
260
}
Employee
[0]
Definition
doc_src_containers.cpp:10
Employee::Employee
Employee()
Definition
doc_src_containers.cpp:12
Employee::name
QString name() const
Definition
src_corelib_tools_qhash.cpp:129
Employee::Employee
Employee(const QString &name, QDate dateOfBirth)
Employee::dateOfBirth
QDate dateOfBirth() const
Definition
src_corelib_tools_qhash.cpp:130
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
__has_include
#define __has_include(x)
Definition
qcompilerdetection.h:459
examples
bool examples()
[3]
Definition
src_corelib_global_qglobal.cpp:56
operator<
bool operator<(const Employee &e1, const Employee &e2)
Definition
src_corelib_tools_qmap.cpp:123
erase
void erase()
[13]
Definition
src_corelib_tools_qmap.cpp:133
qtbase
src
corelib
doc
snippets
code
src_corelib_tools_qmap.cpp
Generated on
for Qt by
1.18.0