summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp')
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp210
1 files changed, 36 insertions, 174 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp
index 145cd10d84..5f87211968 100644
--- a/src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp
@@ -1,52 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the documentation of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** BSD License Usage
-** Alternatively, you may use this file under the terms of the BSD license
-** as follows:
-**
-** "Redistribution and use in source and binary forms, with or without
-** modification, are permitted provided that the following conditions are
-** met:
-** * Redistributions of source code must retain the above copyright
-** notice, this list of conditions and the following disclaimer.
-** * Redistributions in binary form must reproduce the above copyright
-** notice, this list of conditions and the following disclaimer in
-** the documentation and/or other materials provided with the
-** distribution.
-** * Neither the name of The Qt Company Ltd nor the names of its
-** contributors may be used to endorse or promote products derived
-** from this software without specific prior written permission.
-**
-**
-** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
//! [0]
QMap<QString, int> map;
@@ -89,7 +42,7 @@ QMap<int, QWidget *> map;
...
for (int i = 0; i < 1000; ++i) {
if (map[i] == okButton)
- cout << "Found button at index " << i << Qt::endl;
+ cout << "Found button at index " << i << endl;
}
//! [6]
@@ -98,17 +51,14 @@ for (int i = 0; i < 1000; ++i) {
QMapIterator<QString, int> i(map);
while (i.hasNext()) {
i.next();
- cout << i.key() << ": " << i.value() << Qt::endl;
+ cout << qPrintable(i.key()) << ": " << i.value() << endl;
}
//! [7]
//! [8]
-QMap<QString, int>::const_iterator i = map.constBegin();
-while (i != map.constEnd()) {
- cout << i.key() << ": " << i.value() << Qt::endl;
- ++i;
-}
+for (auto i = map.cbegin(), end = map.cend(); i != end; ++i)
+ cout << qPrintable(i.key()) << ": " << i.value() << endl;
//! [8]
@@ -119,27 +69,11 @@ map.insert("plenty", 2000);
//! [9]
-//! [10]
-QList<int> values = map.values("plenty");
-for (int i = 0; i < values.size(); ++i)
- cout << values.at(i) << Qt::endl;
-//! [10]
-
-
-//! [11]
-QMap<QString, int>::iterator i = map.find("plenty");
-while (i != map.end() && i.key() == "plenty") {
- cout << i.value() << Qt::endl;
- ++i;
-}
-//! [11]
-
-
//! [12]
QMap<QString, int> map;
...
-foreach (int value, map)
- cout << value << Qt::endl;
+for (int value : std::as_const(map))
+ cout << value << endl;
//! [12]
@@ -170,43 +104,6 @@ inline bool operator<(const Employee &e1, const Employee &e2)
//! [13]
-//! [14]
-QMap<QString, int> map;
-...
-QMap<QString, int>::const_iterator i = map.find("HDR");
-while (i != map.end() && i.key() == "HDR") {
- cout << i.value() << Qt::endl;
- ++i;
-}
-//! [14]
-
-
-//! [15]
-QMap<int, QString> map;
-map.insert(1, "one");
-map.insert(5, "five");
-map.insert(10, "ten");
-
-map.lowerBound(0); // returns iterator to (1, "one")
-map.lowerBound(1); // returns iterator to (1, "one")
-map.lowerBound(2); // returns iterator to (5, "five")
-map.lowerBound(10); // returns iterator to (10, "ten")
-map.lowerBound(999); // returns end()
-//! [15]
-
-
-//! [16]
-QMap<QString, int> map;
-...
-QMap<QString, int>::const_iterator i = map.lowerBound("HDR");
-QMap<QString, int>::const_iterator upperBound = map.upperBound("HDR");
-while (i != upperBound) {
- cout << i.value() << Qt::endl;
- ++i;
-}
-//! [16]
-
-
//! [17]
QMap<int, QString> map;
map.insert(1, "one");
@@ -228,50 +125,33 @@ map.insert("February", 2);
...
map.insert("December", 12);
-QMap<QString, int>::iterator i;
-for (i = map.begin(); i != map.end(); ++i)
- cout << i.key() << ": " << i.value() << Qt::endl;
+for (auto i = map.cbegin(), end = map.cend(); i != end; ++i)
+ cout << qPrintable(i.key()) << ": " << i.value() << endl;
//! [18]
//! [19]
-QMap<QString, int>::iterator i;
-for (i = map.begin(); i != map.end(); ++i)
+for (auto i = map.begin(), end = map.end(); i != end; ++i)
i.value() += 2;
//! [19]
+void erase()
+{
+QMap<QString, int> map;
//! [20]
-QMap<QString, int>::iterator i = map.begin();
-while (i != map.end()) {
- if (i.key().startsWith('_'))
+QMap<QString, int>::const_iterator i = map.cbegin();
+while (i != map.cend()) {
+ if (i.value() > 10)
i = map.erase(i);
else
++i;
}
//! [20]
-
-
//! [21]
-QMap<QString, int>::iterator i = map.begin();
-while (i != map.end()) {
- QMap<QString, int>::iterator prev = i;
- ++i;
- if (prev.key().startsWith('_'))
- map.erase(prev);
-}
+erase_if(map, [](const QMap<QString, int>::iterator it) { return it.value() > 10; });
//! [21]
-
-
-//! [22]
-// WRONG
-while (i != map.end()) {
- if (i.key().startsWith('_'))
- map.erase(i);
- ++i;
}
-//! [22]
-
//! [23]
if (i.key() == "Hello")
@@ -286,47 +166,16 @@ map.insert("February", 2);
...
map.insert("December", 12);
-QMap<QString, int>::const_iterator i;
-for (i = map.constBegin(); i != map.constEnd(); ++i)
- cout << i.key() << ": " << i.value() << Qt::endl;
+for (auto i = map.cbegin(), end = map.cend(); i != end; ++i)
+ cout << qPrintable(i.key()) << ": " << i.value() << endl;
//! [24]
-//! [25]
-QMultiMap<QString, int> map1, map2, map3;
-
-map1.insert("plenty", 100);
-map1.insert("plenty", 2000);
-// map1.size() == 2
-
-map2.insert("plenty", 5000);
-// map2.size() == 1
-
-map3 = map1 + map2;
-// map3.size() == 3
-//! [25]
-
-
-//! [26]
-QList<int> values = map.values("plenty");
-for (int i = 0; i < values.size(); ++i)
- cout << values.at(i) << Qt::endl;
-//! [26]
-
-
-//! [27]
-QMultiMap<QString, int>::iterator i = map.find("plenty");
-while (i != map.end() && i.key() == "plenty") {
- cout << i.value() << Qt::endl;
- ++i;
-}
-//! [27]
-
//! [keyiterator1]
for (QMap<int, QString>::const_iterator it = map.cbegin(), end = map.cend(); it != end; ++it) {
- cout << "The key: " << it.key() << Qt::endl
- cout << "The value: " << it.value() << Qt::endl;
- cout << "Also the value: " << (*it) << Qt::endl;
+ cout << "The key: " << it.key() << endl;
+ cout << "The value: " << qPrintable(it.value()) << endl;
+ cout << "Also the value: " << qPrintable(*it) << endl;
}
//! [keyiterator1]
@@ -340,3 +189,16 @@ qDeleteAll(map2.keys());
int numPrimes = std::count_if(map.keyBegin(), map.keyEnd(), isPrimeNumber);
qDeleteAll(map2.keyBegin(), map2.keyEnd());
//! [keyiterator2]
+
+//! [28]
+QMap<QString, int> map;
+map.insert("January", 1);
+map.insert("February", 2);
+// ...
+map.insert("December", 12);
+
+for (auto [key, value] : map.asKeyValueRange()) {
+ cout << qPrintable(key) << ": " << value << endl;
+ --value; // convert to JS month indexing
+}
+//! [28]