Qt 6.x
The Qt SDK
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
7
8
10map["one"] = 1;
11map["three"] = 3;
12map["seven"] = 7;
14
15
17map.insert("twelve", 12);
19
20
22int num1 = map["thirteen"];
23int num2 = map.value("thirteen");
25
26
28int timeout = 30;
29if (map.contains("TIMEOUT"))
30 timeout = map.value("TIMEOUT");
32
33
35int timeout = map.value("TIMEOUT", 30);
37
38
40// WRONG
42...
43for (int i = 0; i < 1000; ++i) {
44 if (map[i] == okButton)
45 cout << "Found button at index " << i << endl;
46}
48
49
51QMapIterator<QString, int> i(map);
52while (i.hasNext()) {
53 i.next();
54 cout << qPrintable(i.key()) << ": " << i.value() << endl;
55}
57
58
60for (auto i = map.cbegin(), end = map.cend(); i != end; ++i)
61 cout << qPrintable(i.key()) << ": " << i.value() << endl;
63
64
66map.insert("plenty", 100);
67map.insert("plenty", 2000);
68// map.value("plenty") == 2000
70
71
74...
75for (int value : std::as_const(map))
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 if (e1.name() != e2.name())
99 return e1.name() < e2.name();
100 return e1.dateOfBirth() < e2.dateOfBirth();
101}
102
103#endif // EMPLOYEE_H
105
106
109map.insert(1, "one");
110map.insert(5, "five");
111map.insert(10, "ten");
112
113map.upperBound(0); // returns iterator to (1, "one")
114map.upperBound(1); // returns iterator to (5, "five")
115map.upperBound(2); // returns iterator to (5, "five")
116map.upperBound(10); // returns end()
117map.upperBound(999); // returns end()
119
120
123map.insert("January", 1);
124map.insert("February", 2);
125...
126map.insert("December", 12);
127
128for (auto i = map.cbegin(), end = map.cend(); i != end; ++i)
129 cout << qPrintable(i.key()) << ": " << i.value() << endl;
131
132
134for (auto i = map.begin(), end = map.end(); i != end; ++i)
135 i.value() += 2;
137
138
139void erase()
140{
144while (i != map.cend()) {
145 if (i.value() > 10)
146 i = map.erase(i);
147 else
148 ++i;
149}
152erase_if(map, [](const QMap<QString, int>::iterator it) { return it.value() > 10; });
154}
155
157if (i.key() == "Hello")
158 i.value() = "Bonjour";
160
161
164map.insert("January", 1);
165map.insert("February", 2);
166...
167map.insert("December", 12);
168
169for (auto i = map.cbegin(), end = map.cend(); i != end; ++i)
170 cout << qPrintable(i.key()) << ": " << i.value() << endl;
172
173
176 cout << "The key: " << it.key() << endl;
177 cout << "The value: " << qPrintable(it.value()) << endl;
178 cout << "Also the value: " << qPrintable(*it) << endl;
179}
181
183// Inefficient, keys() is expensive
185int numPrimes = std::count_if(map.cbegin(), map.cend(), isPrimeNumber);
186qDeleteAll(map2.keys());
187
188// Efficient, no memory allocation needed
189int numPrimes = std::count_if(map.keyBegin(), map.keyEnd(), isPrimeNumber);
190qDeleteAll(map2.keyBegin(), map2.keyEnd());
192
195map.insert("January", 1);
196map.insert("February", 2);
197// ...
198map.insert("December", 12);
199
200for (auto [key, value] : map.asKeyValueRange()) {
201 cout << qPrintable(key) << ": " << value << endl;
202 --value; // convert to JS month indexing
203}
QString name() const
[5] //! [6]
Definition employee.h:49
\inmodule QtCore \reentrant
Definition qdatetime.h:27
Definition qlist.h:74
Definition qmap.h:186
iterator insert(const Key &key, const T &value)
Definition qmap.h:687
T value(const Key &key, const T &defaultValue=T()) const
Definition qmap.h:356
iterator erase(const_iterator it)
Definition qmap.h:618
bool contains(const Key &key) const
Definition qmap.h:340
const_iterator cend() const
Definition qmap.h:604
const_iterator cbegin() const
Definition qmap.h:600
QList< Key > keys() const
Definition qmap.h:382
iterator begin()
Definition qmap.h:597
iterator end()
Definition qmap.h:601
iterator upperBound(const Key &key)
Definition qmap.h:673
key_iterator keyBegin() const
Definition qmap.h:605
key_iterator keyEnd() const
Definition qmap.h:606
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
qDeleteAll(list.begin(), list.end())
QSet< QString >::iterator it
QTextStream & endl(QTextStream &stream)
Writes '\n' to the stream and flushes the stream.
qsizetype erase_if(QByteArray &ba, Predicate pred)
Definition qbytearray.h:701
qsizetype erase(QByteArray &ba, const T &t)
Definition qbytearray.h:695
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
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:1391
QStringList keys
QMap< QString, int > map
[0]
int num1
[2]