Brush up the container code snippets

- Bring iterator loops into a consistent form using auto and
  creating and end variable, use cbegin()/cend() where suitable
- Use (std::)endl instead of Qt::endl for iostreams
- Fix removed container conversion API (QList::fromSet, QSet::toList())
- Use range-based for instead of foreach
- Use initializer lists
- Use qPrintable(QString) for output to std::ostream
- Use qsizetype
- Remove some unused snippets

Complements f6b137bdc4.

Pick-to: 6.5
Change-Id: I8a167099cdb224f45b984fa834d46269144a7ef0
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
bb10
Friedemann Kleint 2023-04-05 15:13:00 +02:00
parent 5c71312ba3
commit 7d542e1daf
15 changed files with 88 additions and 114 deletions

View File

@ -34,18 +34,15 @@ while (i.hasNext()) {
//! [5]
QSet<QWidget *>::const_iterator i = set.constBegin();
while (i != set.constEnd()) {
for (auto i = set.cbegin(), end = set.cend(); != end; ++i)
qDebug() << *i;
++i;
}
//! [5]
//! [6]
QSet<QString> set;
...
foreach (const QString &value, set)
for (const auto &value : set)
qDebug() << value;
//! [6]
@ -59,20 +56,18 @@ for (int i = 0; i < 20000; ++i)
//! [8]
QSet<QString> set;
set << "January" << "February" << ... << "December";
QSet<QString> set = {"January", "February", ... "December"}
QSet<QString>::iterator i;
for (i = set.begin(); i != set.end(); ++i)
// i is a QSet<QString>::iterator
for (auto i = set.begin(), end = set.end(); i != end; ++i)
qDebug() << *i;
//! [8]
//! [9]
QSet<QString> set;
set << "January" << "February" << ... << "December";
QSet<QString> set = {"January", "February", ... "December"};
QSet<QString>::iterator i = set.begin();
auto i = set.begin();
while (i != set.end()) {
if ((*i).startsWith('J')) {
i = set.erase(i);
@ -94,11 +89,10 @@ if (it != set.end())
//! [11]
QSet<QString> set;
set << "January" << "February" << ... << "December";
QSet<QString> set = {"January", "February", ... "December"};
QSet<QString>::const_iterator i;
for (i = set.begin(); i != set.end(); ++i)
// i is QSet<QString>::const_iterator
for (auto i = set.cbegin(), end = set.cend(); i != end; ++i)
qDebug() << *i;
//! [11]

View File

@ -138,10 +138,10 @@ QList<Login> logins;
QSettings settings;
settings.beginWriteArray("logins");
for (int i = 0; i < logins.size(); ++i) {
for (qsizetype i = 0; i < logins.size(); ++i) {
settings.setArrayIndex(i);
settings.setValue("userName", list.at(i).userName);
settings.setValue("password", list.at(i).password);
settings.setValue("userName", logins.at(i).userName);
settings.setValue("password", logins.at(i).password);
}
settings.endArray();
//! [16]

View File

@ -198,7 +198,7 @@ using namespace Qt::StringLiterals;
void appendMap(QCborStreamWriter &writer, const QMap<int, QString> &map)
{
writer.startMap(map.size());
for (auto it = map.begin(); it != map.end(); ++it) {
for (auto it = map.cbegin(), end = map.cend(); it != end; ++it) {
writer.append(it.key());
writer.append(it.value());
}

View File

@ -24,7 +24,7 @@ ba[4] = 0xca;
//! [2]
for (qsizetype i = 0; i < ba.size(); ++i) {
if (ba.at(i) >= 'a' && ba.at(i) <= 'f')
cout << "Found character in range [a-f]" << Qt::endl;
cout << "Found character in range [a-f]" << endl;
}
//! [2]
@ -41,7 +41,7 @@ x.replace(5, 3, "&"); // x == "rock & roll"
QByteArray ba("We must be <b>bold</b>, very <b>bold</b>");
qsizetype j = 0;
while ((j = ba.indexOf("<b>", j)) != -1) {
cout << "Found <b> tag at index position " << j << Qt::endl;
cout << "Found <b> tag at index position " << j << endl;
++j;
}
//! [4]
@ -79,7 +79,7 @@ QByteArray("abc").isEmpty(); // returns false
QByteArray ba("Hello world");
char *data = ba.data();
while (*data) {
cout << "[" << *data << "]" << Qt::endl;
cout << "[" << *data << "]" << endl;
++data;
}
//! [8]

View File

@ -6,7 +6,7 @@ QFuture<QString> future = ...;
QFuture<QString>::const_iterator i;
for (i = future.constBegin(); i != future.constEnd(); ++i)
cout << *i << Qt::endl;
cout << qPrintable(*i) << endl;
//! [0]

View File

@ -42,7 +42,7 @@ QHash<int, QWidget *> hash;
...
for (int i = 0; i < 1000; ++i) {
if (hash[i] == okButton)
cout << "Found button at index " << i << Qt::endl;
cout << "Found button at index " << i << endl;
}
//! [6]
@ -51,17 +51,14 @@ for (int i = 0; i < 1000; ++i) {
QHashIterator<QString, int> i(hash);
while (i.hasNext()) {
i.next();
cout << i.key() << ": " << i.value() << Qt::endl;
cout << qPrintable(i.key()) << ": " << i.value() << endl;
}
//! [7]
//! [8]
QHash<QString, int>::const_iterator i = hash.cbegin();
while (i != hash.cend()) {
cout << i.key() << ": " << i.value() << Qt::endl;
++i;
}
for (auto i = map.cbegin(), end = map.cend(); != end; ++i)
cout << qPrintable(i.key()) << ": " << i.value() << endl;
//! [8]
@ -75,8 +72,8 @@ hash.insert("plenty", 2000);
//! [12]
QHash<QString, int> hash;
...
foreach (int value, hash)
cout << value << Qt::endl;
for (int value : std::as_const(hash))
cout << value << endl;
//! [12]
@ -138,7 +135,7 @@ QHash<QString, int> hash;
...
QHash<QString, int>::const_iterator i = hash.find("HDR");
while (i != hash.end() && i.key() == "HDR") {
cout << i.value() << Qt::endl;
cout << i.value() << endl;
++i;
}
//! [16]
@ -151,15 +148,13 @@ hash.insert("February", 2);
...
hash.insert("December", 12);
QHash<QString, int>::iterator i;
for (i = hash.begin(); i != hash.end(); ++i)
cout << i.key() << ": " << i.value() << Qt::endl;
for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i)
cout << qPrintable(key()) << ": " << i.value() << endl;
//! [17]
//! [18]
QHash<QString, int>::iterator i;
for (i = hash.begin(); i != hash.end(); ++i)
for (auto i = hash.begin(), end = hash.end(); i != end; ++i)
i.value() += 2;
//! [18]
@ -181,9 +176,8 @@ hash.insert("February", 2);
...
hash.insert("December", 12);
QHash<QString, int>::const_iterator i;
for (i = hash.cbegin(); i != hash.cend(); ++i)
cout << i.key() << ": " << i.value() << Qt::endl;
for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i)
cout << qPrintable(i.key()) << ": " << i.value() << endl;
//! [23]
@ -204,24 +198,24 @@ hash3 = hash1 + hash2;
//! [25]
QList<int> values = hash.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;
//! [25]
//! [26]
QMultiHash<QString, int>::iterator i = hash.find("plenty");
while (i != hash.end() && i.key() == "plenty") {
cout << i.value() << Qt::endl;
auto i = hash.constFind("plenty");
while (i != hash.cend() && i.key() == "plenty") {
cout << i.value() << endl;
++i;
}
//! [26]
//! [27]
for (QHash<int, QString>::const_iterator it = hash.cbegin(), end = hash.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 = hash.cbegin(), end = hash.cend(); it != end; ++it) {
cout << "The key: " << it.key() << endl;
cout << "The value: " << qPrintable(it.value()) << endl;
cout << "Also the value: " << qPrintable(*it) << endl;
}
//! [27]
@ -294,7 +288,7 @@ hash.insert("February", 2);
hash.insert("December", 12);
for (auto [key, value] : hash.asKeyValueRange()) {
cout << key << ": " << value << Qt::endl;
cout << qPrintable(key) << ": " << value << endl;
--value; // convert to JS month indexing
}
//! [34]
@ -307,7 +301,7 @@ hash.insert("February", 2);
hash.insert("December", 12);
for (auto [key, value] : hash.asKeyValueRange()) {
cout << key << ": " << value << Qt::endl;
cout << qPrintable(key) << ": " << value << endl;
--value; // convert to JS month indexing
}
//! [35]

View File

@ -26,7 +26,7 @@ if (list[0] == "Liz")
//! [4]
for (qsizetype i = 0; i < list.size(); ++i) {
if (list.at(i) == "Alfonso")
cout << "Found Alfonso at position " << i << Qt::endl;
cout << "Found Alfonso at position " << i << endl;
}
//! [4]
@ -34,7 +34,7 @@ for (qsizetype i = 0; i < list.size(); ++i) {
//! [5]
qsizetype i = list.indexOf("Harumi");
if (i != -1)
cout << "First occurrence of Harumi is at position " << i << Qt::endl;
cout << "First occurrence of Harumi is at position " << i << endl;
//! [5]
@ -101,16 +101,14 @@ list.prepend("three");
//! [9]
QList<QString> list;
list << "alpha" << "beta" << "delta";
QList<QString> list = {"alpha", "beta", "delta"};
list.insert(2, "gamma");
// list: ["alpha", "beta", "gamma", "delta"]
//! [9]
//! [10]
QList<double> list;
list << 2.718 << 1.442 << 0.4342;
QList<double> list = {2.718, 1.442, 0.4342};
list.insert(1, 3, 9.9);
// list: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342]
//! [10]
@ -127,8 +125,7 @@ list.fill("oh", 5);
//! [12]
QList<QString> list;
list << "A" << "B" << "C" << "B" << "A";
QList<QString> list{"A", "B", "C", "B", "A"};
list.indexOf("B"); // returns 1
list.indexOf("B", 1); // returns 1
list.indexOf("B", 2); // returns 3
@ -137,8 +134,7 @@ list.indexOf("X"); // returns -1
//! [13]
QList<QString> list;
list << "A" << "B" << "C" << "B" << "A";
QList<QString> list = {"A", "B", "C", "B", "A"};
list.lastIndexOf("B"); // returns 3
list.lastIndexOf("B", 3); // returns 3
list.lastIndexOf("B", 2); // returns 1

View File

@ -42,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]
@ -51,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.cbegin();
while (i != map.cend()) {
cout << i.key() << ": " << i.value() << Qt::endl;
++i;
}
for (auto i = map.cbegin(), end = map.cend(); != end; ++i)
cout << qPrintable(i.key()) << ": " << i.value() << endl;
//! [8]
@ -75,8 +72,8 @@ map.insert("plenty", 2000);
//! [12]
QMap<QString, int> map;
...
foreach (int value, map)
cout << value << Qt::endl;
for (int value : std::as_const(map))
cout << value << endl;
//! [12]
@ -128,15 +125,13 @@ 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]
@ -171,17 +166,16 @@ map.insert("February", 2);
...
map.insert("December", 12);
QMap<QString, int>::const_iterator i;
for (i = map.cbegin(); i != map.cend(); ++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]
//! [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]
@ -204,7 +198,7 @@ map.insert("February", 2);
map.insert("December", 12);
for (auto [key, value] : map.asKeyValueRange()) {
cout << key << ": " << value << Qt::endl;
cout << qPrintable(key) << ": " << value << endl;
--value; // convert to JS month indexing
}
//! [28]

View File

@ -19,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]
@ -47,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.cbegin();
while (i != multimap.cend()) {
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]
@ -70,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]
@ -94,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]
@ -149,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]
@ -207,9 +204,8 @@ multimap.insert("February", 2);
...
multimap.insert("December", 12);
QMultiMap<QString, int>::const_iterator i;
for (i = multimap.cbegin(); i != multimap.cend(); ++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]
@ -228,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]
@ -254,7 +250,7 @@ map.insert("February", 2);
map.insert("December", 12);
for (auto [key, value] : map.asKeyValueRange()) {
cout << key << ": " << value << Qt::endl;
cout << qPrintable(key) << ": " << value << endl;
--value; // convert to JS month indexing
}
//! [26]

View File

@ -7,5 +7,5 @@ queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
while (!queue.isEmpty())
cout << queue.dequeue() << Qt::endl;
cout << queue.dequeue() << endl;
//! [0]

View File

@ -66,7 +66,7 @@ Widget::Widget(QWidget *parent)
result.clear();
//! [12]
foreach (const QString &str, list) {
for (const auto &str : std::as_const(list)) {
if (str.contains("Bill"))
result += str;
}

View File

@ -1640,7 +1640,7 @@ size_t qHash(long double key, size_t seed) noexcept
hash table, use \l{QMultiHash}.
If you only need to extract the values from a hash (not the keys),
you can also use \l{foreach}:
you can also use range-based for:
\snippet code/src_corelib_tools_qhash.cpp 12

View File

@ -97,7 +97,7 @@
QMultiMap.
If you only need to extract the values from a map (not the keys),
you can also use \l{foreach}:
you can also use range-based for:
\snippet code/src_corelib_tools_qmap.cpp 12

View File

@ -92,7 +92,7 @@
\snippet code/src_corelib_tools_qmultimap.cpp 11
If you only need to extract the values from a map (not the keys),
you can also use \l{foreach}:
you can also use range-based for:
\snippet code/src_corelib_tools_qmultimap.cpp 12

View File

@ -46,7 +46,7 @@
QSet is unordered, so an iterator's sequence cannot be assumed to
be predictable. If ordering by key is required, use a QMap.
To navigate through a QSet, you can also use \l{foreach}:
To navigate through a QSet, you can also use range-based for:
\snippet code/doc_src_qset.cpp 6