diff options
Diffstat (limited to 'src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp')
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp | 154 |
1 files changed, 43 insertions, 111 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp index 9c2a01834b..42ec46585b 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp @@ -1,53 +1,6 @@ -/**************************************************************************** -** -** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> -** 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) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause //! [0] QMultiMap<QString, int> multimap; @@ -66,8 +19,8 @@ multimap.insert("c", -5); int num2 = multimap.value("a"); // 1 int num3 = multimap.value("thirteen"); // not found; 0 int num3 = 0; -auto it = multimap.value("b"); -if (it != multimap.end()) { +auto it = multimap.constFind("b"); +if (it != multimap.cend()) { num3 = it.value(); } //! [3] @@ -94,17 +47,14 @@ int timeout = multimap.value("TIMEOUT", 30); QMultiMapIterator<QString, int> i(multimap); while (i.hasNext()) { i.next(); - cout << i.key() << ": " << i.value() << Qt::endl; + cout << qPrintable(i.key()) << ": " << i.value() << endl; } //! [7] //! [8] -auto i = multimap.constBegin(); -while (i != multimap.constEnd()) { - cout << i.key() << ": " << i.value() << Qt::endl; - ++i; -} +for (auto i = multimap.cbegin(), end = multimap.cend(); i != end; ++i) + cout << qPrintable(i.key()) << ": " << i.value() << endl; //! [8] @@ -117,22 +67,22 @@ multimap.insert("plenty", 2000); //! [10] QList<int> values = multimap.values("plenty"); -for (int i = 0; i < values.size(); ++i) - cout << values.at(i) << Qt::endl; +for (auto i : std::as_const(values)) + cout << i << endl; //! [10] //! [11] -QMultiMap<QString, int>::iterator i = multimap.find("plenty"); +auto i = multimap.find("plenty"); while (i != map.end() && i.key() == "plenty") { - cout << i.value() << Qt::endl; + cout << i.value() << endl; ++i; } // better: auto [i, end] = multimap.equal_range("plenty"); while (i != end) { - cout << i.value() << Qt::endl; + cout << i.value() << endl; ++i; } //! [11] @@ -141,8 +91,8 @@ while (i != end) { //! [12] QMap<QString, int> multimap; ... -foreach (int value, multimap) - cout << value << Qt::endl; +for (int value : std::as_const(multimap)) + cout << value << endl; //! [12] @@ -196,7 +146,7 @@ QMap<QString, int> multimap; QMap<QString, int>::const_iterator i = multimap.lowerBound("HDR"); QMap<QString, int>::const_iterator upperBound = multimap.upperBound("HDR"); while (i != upperBound) { - cout << i.value() << Qt::endl; + cout << i.value() << endl; ++i; } //! [16] @@ -218,57 +168,27 @@ multimap.upperBound(10); // returns end() multimap.upperBound(999); // returns end() //! [17] - -//! [18] -QMultiMap<QString, int> multimap; -multimap.insert("January", 1); -multimap.insert("February", 2); -... -multimap.insert("December", 12); - -QMap<QString, int>::iterator i; -for (i = multimap.begin(); i != multimap.end(); ++i) - cout << i.key() << ": " << i.value() << Qt::endl; -//! [18] - - //! [19] -QMultiMap<QString, int>::iterator i; -for (i = multimap.begin(); i != multimap.end(); ++i) +for (auto it = multimap.begin(), end = multimap.end(); i != end; ++i) i.value() += 2; //! [19] - +void erase() +{ +QMultiMap<QString, int> multimap; //! [20] -QMultiMap<QString, int>::iterator i = multimap.begin(); -while (i != multimap.end()) { - if (i.key().startsWith('_')) +QMultiMap<QString, int>::const_iterator i = multimap.cbegin(); +while (i != multimap.cend()) { + if (i.value() > 10) i = multimap.erase(i); else ++i; } //! [20] - - //! [21] -QMultiMap<QString, int>::iterator i = multimap.begin(); -while (i != multimap.end()) { - QMap<QString, int>::iterator prev = i; - ++i; - if (prev.key().startsWith('_')) - multimap.erase(prev); -} +erase_if(multimap, [](const QMultiMap<QString, int>::iterator it) { return it.value() > 10; }); //! [21] - - -//! [22] -// WRONG -while (i != multimap.end()) { - if (i.key().startsWith('_')) - multimap.erase(i); - ++i; } -//! [22] //! [23] @@ -284,9 +204,8 @@ multimap.insert("February", 2); ... multimap.insert("December", 12); -QMultiMap<QString, int>::const_iterator i; -for (i = multimap.constBegin(); i != multimap.constEnd(); ++i) - cout << i.key() << ": " << i.value() << Qt::endl; +for (auto i = multimap.cbegin(), end = multimap.cend(); i != end; ++i) + cout << qPrintable(i.key()) << ": " << i.value() << endl; //! [24] @@ -305,10 +224,10 @@ map3 = map1 + map2; //! [25] //! [keyiterator1] -for (QMultiMap<int, QString>::const_iterator it = multimap.cbegin(), end = multimap.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; +for (auto it = multimap.cbegin(), end = multimap.cend(); it != end; ++it) { + cout << "The key: " << it.key() << endl + cout << "The value: " << qPrintable(it.value()) << endl; + cout << "Also the value: " << qPrintable(*it) << endl; } //! [keyiterator1] @@ -322,3 +241,16 @@ qDeleteAll(multimap2.keys()); int numPrimes = std::count_if(multimap.keyBegin(), multimap.keyEnd(), isPrimeNumber); qDeleteAll(multimap2.keyBegin(), multimap2.keyEnd()); //! [keyiterator2] + +//! [26] +QMultiMap<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 +} +//! [26] |