QtBase/doc: fix examples for QHash/QMap/QMultiMap::erase()

QHash/MultiHash & QMap/MultiMap::erase() do no longer take an iterator
in Qt6. Clean up the examples by only providing one example, the rest is
common c++ which should not be handled in the Qt documentation. Also
mention erase_if() and remove references to the (soon to be deprecated)
Java-style iterators.

Pick-to: 6.5 6.2
Fixes: QTBUG-105109
Change-Id: I47b11f3b5dcc173494e5c6f9ad0167c613b12209
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Christian Ehrlicher 2023-03-19 19:23:42 +01:00
parent d25337a631
commit 3236b64db8
6 changed files with 77 additions and 239 deletions

View File

@ -57,8 +57,8 @@ while (i.hasNext()) {
//! [8]
QHash<QString, int>::const_iterator i = hash.constBegin();
while (i != hash.constEnd()) {
QHash<QString, int>::const_iterator i = hash.cbegin();
while (i != hash.cend()) {
cout << i.key() << ": " << i.value() << Qt::endl;
++i;
}
@ -163,38 +163,22 @@ for (i = hash.begin(); i != hash.end(); ++i)
i.value() += 2;
//! [18]
//! [19]
QHash<QString, int>::iterator i = hash.begin();
while (i != hash.end()) {
if (i.key().startsWith('_'))
void erase()
{
QHash<QString, int> hash;
//! [20]
QHash<QString, int>::const_iterator i = hash.cbegin();
while (i != hash.cend()) {
if (i.value() > 10)
i = hash.erase(i);
else
++i;
}
//! [19]
//! [20]
QHash<QString, int>::iterator i = hash.begin();
while (i != hash.end()) {
QHash<QString, int>::iterator prev = i;
++i;
if (prev.key().startsWith('_'))
hash.erase(prev);
}
//! [20]
//! [21]
// WRONG
while (i != hash.end()) {
if (i.key().startsWith('_'))
hash.erase(i);
++i;
}
erase_if(hash, [](const QHash<QString, int>::iterator it) { return it.value() > 10; });
//! [21]
}
//! [22]
if (i.key() == "Hello")
@ -210,7 +194,7 @@ hash.insert("February", 2);
hash.insert("December", 12);
QHash<QString, int>::const_iterator i;
for (i = hash.constBegin(); i != hash.constEnd(); ++i)
for (i = hash.cbegin(); i != hash.cend(); ++i)
cout << i.key() << ": " << i.value() << Qt::endl;
//! [23]

View File

@ -57,8 +57,8 @@ while (i.hasNext()) {
//! [8]
QMap<QString, int>::const_iterator i = map.constBegin();
while (i != map.constEnd()) {
QMap<QString, int>::const_iterator i = map.cbegin();
while (i != map.cend()) {
cout << i.key() << ": " << i.value() << Qt::endl;
++i;
}
@ -178,37 +178,22 @@ for (i = map.begin(); i != map.end(); ++i)
//! [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")
@ -224,7 +209,7 @@ map.insert("February", 2);
map.insert("December", 12);
QMap<QString, int>::const_iterator i;
for (i = map.constBegin(); i != map.constEnd(); ++i)
for (i = map.cbegin(); i != map.cend(); ++i)
cout << i.key() << ": " << i.value() << Qt::endl;
//! [24]

View File

@ -53,8 +53,8 @@ while (i.hasNext()) {
//! [8]
auto i = multimap.constBegin();
while (i != multimap.constEnd()) {
auto i = multimap.cbegin();
while (i != multimap.cend()) {
cout << i.key() << ": " << i.value() << Qt::endl;
++i;
}
@ -185,43 +185,22 @@ for (i = multimap.begin(); i != multimap.end(); ++i)
//! [18]
//! [19]
QMultiMap<QString, int>::iterator i;
for (i = multimap.begin(); i != multimap.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]
@ -238,7 +217,7 @@ multimap.insert("February", 2);
multimap.insert("December", 12);
QMultiMap<QString, int>::const_iterator i;
for (i = multimap.constBegin(); i != multimap.constEnd(); ++i)
for (i = multimap.cbegin(); i != multimap.cend(); ++i)
cout << i.key() << ": " << i.value() << Qt::endl;
//! [24]

View File

@ -2467,12 +2467,6 @@ size_t qHash(long double key, size_t seed) noexcept
\inmodule QtCore
\brief The QHash::iterator class provides an STL-style non-const iterator for QHash.
QHash features both \l{STL-style iterators} and \l{Java-style
iterators}. The STL-style iterators are more low-level and more
cumbersome to use; on the other hand, they are slightly faster
and, for developers who already know STL, have the advantage of
familiarity.
QHash\<Key, T\>::iterator allows you to iterate over a QHash
and to modify the value (but not the key) associated
with a particular key. If you want to iterate over a const QHash,
@ -2493,31 +2487,15 @@ size_t qHash(long double key, size_t seed) noexcept
Unlike QMap, which orders its items by key, QHash stores its
items in an arbitrary order.
Let's see a few examples of things we can do with a
QHash::iterator that we cannot do with a QHash::const_iterator.
Here's an example that increments every value stored in the QHash
by 2:
\snippet code/src_corelib_tools_qhash.cpp 18
Here's an example that removes all the items whose key is a
string that starts with an underscore character:
\snippet code/src_corelib_tools_qhash.cpp 19
The call to QHash::erase() removes the item pointed to by the
iterator from the hash, and returns an iterator to the next item.
Here's another way of removing an item while iterating:
\snippet code/src_corelib_tools_qhash.cpp 20
It might be tempting to write code like this:
To remove elements from a QHash you can use erase_if(QHash\<Key, T\> &map, Predicate pred):
\snippet code/src_corelib_tools_qhash.cpp 21
However, this will potentially crash in \c{++i}, because \c i is
a dangling iterator after the call to erase().
Multiple iterators can be used on the same hash. However, be aware
that any modification performed directly on the QHash (inserting and
removing items) can cause the iterators to become invalid.
@ -2528,10 +2506,6 @@ size_t qHash(long double key, size_t seed) noexcept
to grow/shrink its internal hash table.
Using any iterator after a rehashing operation has occurred will lead to undefined behavior.
You can however safely use iterators to remove entries from the hash
using the QHash::erase() method. This function can safely be called while
iterating, and won't affect the order of items in the hash.
If you need to keep iterators over a long period of time, we recommend
that you use QMap rather than QHash.
@ -2540,7 +2514,7 @@ size_t qHash(long double key, size_t seed) noexcept
while iterators are active on that container. For more information,
read \l{Implicit sharing iterator problem}.
\sa QHash::const_iterator, QHash::key_iterator, QMutableHashIterator
\sa QHash::const_iterator, QHash::key_iterator, QHash::key_value_iterator
*/
/*! \fn template <class Key, class T> QHash<Key, T>::iterator::iterator()
@ -2636,12 +2610,6 @@ size_t qHash(long double key, size_t seed) noexcept
\inmodule QtCore
\brief The QHash::const_iterator class provides an STL-style const iterator for QHash.
QHash features both \l{STL-style iterators} and \l{Java-style
iterators}. The STL-style iterators are more low-level and more
cumbersome to use; on the other hand, they are slightly faster
and, for developers who already know STL, have the advantage of
familiarity.
QHash\<Key, T\>::const_iterator allows you to iterate over a
QHash. If you want to modify the QHash as you
iterate over it, you must use QHash::iterator instead. It is
@ -2652,8 +2620,8 @@ size_t qHash(long double key, size_t seed) noexcept
The default QHash::const_iterator constructor creates an
uninitialized iterator. You must initialize it using a QHash
function like QHash::constBegin(), QHash::constEnd(), or
QHash::find() before you can start iterating. Here's a typical
function like QHash::cbegin(), QHash::cend(), or
QHash::constFind() before you can start iterating. Here's a typical
loop that prints all the (key, value) pairs stored in a hash:
\snippet code/src_corelib_tools_qhash.cpp 23
@ -2674,12 +2642,16 @@ size_t qHash(long double key, size_t seed) noexcept
to grow/shrink its internal hash table.
Using any iterator after a rehashing operation has occurred will lead to undefined behavior.
You can however safely use iterators to remove entries from the hash
using the QHash::erase() method. This function can safely be called while
iterating, and won't affect the order of items in the hash.
\warning Iterators on implicitly shared containers do not work
exactly like STL-iterators. You should avoid copying a container
while iterators are active on that container. For more information,
read \l{Implicit sharing iterator problem}.
\sa QHash::iterator, QHashIterator
\sa QHash::iterator, QHash::key_iterator, QHash::const_key_value_iterator
*/
/*! \fn template <class Key, class T> QHash<Key, T>::const_iterator::const_iterator()
@ -3435,12 +3407,6 @@ size_t qHash(long double key, size_t seed) noexcept
\inmodule QtCore
\brief The QMultiHash::iterator class provides an STL-style non-const iterator for QMultiHash.
QMultiHash features both \l{STL-style iterators} and \l{Java-style
iterators}. The STL-style iterators are more low-level and more
cumbersome to use; on the other hand, they are slightly faster
and, for developers who already know STL, have the advantage of
familiarity.
QMultiHash\<Key, T\>::iterator allows you to iterate over a QMultiHash
and to modify the value (but not the key) associated
with a particular key. If you want to iterate over a const QMultiHash,
@ -3461,31 +3427,15 @@ size_t qHash(long double key, size_t seed) noexcept
Unlike QMap, which orders its items by key, QMultiHash stores its
items in an arbitrary order.
Let's see a few examples of things we can do with a
QMultiHash::iterator that we cannot do with a QMultiHash::const_iterator.
Here's an example that increments every value stored in the QMultiHash
by 2:
\snippet code/src_corelib_tools_qhash.cpp 18
Here's an example that removes all the items whose key is a
string that starts with an underscore character:
\snippet code/src_corelib_tools_qhash.cpp 19
The call to QMultiHash::erase() removes the item pointed to by the
iterator from the hash, and returns an iterator to the next item.
Here's another way of removing an item while iterating:
\snippet code/src_corelib_tools_qhash.cpp 20
It might be tempting to write code like this:
To remove elements from a QMultiHash you can use erase_if(QMultiHash\<Key, T\> &map, Predicate pred):
\snippet code/src_corelib_tools_qhash.cpp 21
However, this will potentially crash in \c{++i}, because \c i is
a dangling iterator after the call to erase().
Multiple iterators can be used on the same hash. However, be aware
that any modification performed directly on the QHash (inserting and
removing items) can cause the iterators to become invalid.
@ -3496,10 +3446,6 @@ size_t qHash(long double key, size_t seed) noexcept
to grow/shrink its internal hash table.
Using any iterator after a rehashing operation has occurred will lead to undefined behavior.
You can however safely use iterators to remove entries from the hash
using the QHash::erase() method. This function can safely be called while
iterating, and won't affect the order of items in the hash.
If you need to keep iterators over a long period of time, we recommend
that you use QMultiMap rather than QHash.
@ -3508,7 +3454,7 @@ size_t qHash(long double key, size_t seed) noexcept
while iterators are active on that container. For more information,
read \l{Implicit sharing iterator problem}.
\sa QMultiHash::const_iterator, QMultiHash::key_iterator, QMutableHashIterator
\sa QMultiHash::const_iterator, QMultiHash::key_iterator, QMultiHash::key_value_iterator
*/
/*! \fn template <class Key, class T> QMultiHash<Key, T>::iterator::iterator()
@ -3604,12 +3550,6 @@ size_t qHash(long double key, size_t seed) noexcept
\inmodule QtCore
\brief The QMultiHash::const_iterator class provides an STL-style const iterator for QMultiHash.
QMultiHash features both \l{STL-style iterators} and \l{Java-style
iterators}. The STL-style iterators are more low-level and more
cumbersome to use; on the other hand, they are slightly faster
and, for developers who already know STL, have the advantage of
familiarity.
QMultiHash\<Key, T\>::const_iterator allows you to iterate over a
QMultiHash. If you want to modify the QMultiHash as you
iterate over it, you must use QMultiHash::iterator instead. It is
@ -3620,8 +3560,8 @@ size_t qHash(long double key, size_t seed) noexcept
The default QMultiHash::const_iterator constructor creates an
uninitialized iterator. You must initialize it using a QMultiHash
function like QMultiHash::constBegin(), QMultiHash::constEnd(), or
QMultiHash::find() before you can start iterating. Here's a typical
function like QMultiHash::cbegin(), QMultiHash::cend(), or
QMultiHash::constFind() before you can start iterating. Here's a typical
loop that prints all the (key, value) pairs stored in a hash:
\snippet code/src_corelib_tools_qhash.cpp 23
@ -3633,28 +3573,24 @@ size_t qHash(long double key, size_t seed) noexcept
recently to the least recently inserted value.
Multiple iterators can be used on the same hash. However, be aware
that any modification performed directly on the QHash (inserting and
that any modification performed directly on the QMultiHash (inserting and
removing items) can cause the iterators to become invalid.
Inserting items into the hash or calling methods such as QHash::reserve()
or QHash::squeeze() can invalidate all iterators pointing into the hash.
Iterators are guaranteed to stay valid only as long as the QHash doesn't have
Inserting items into the hash or calling methods such as QMultiHash::reserve()
or QMultiHash::squeeze() can invalidate all iterators pointing into the hash.
Iterators are guaranteed to stay valid only as long as the QMultiHash doesn't have
to grow/shrink it's internal hash table.
Using any iterator after a rehashing operation ahs occurred will lead to undefined behavior.
You can however safely use iterators to remove entries from the hash
using the QHash::erase() method. This function can safely be called while
iterating, and won't affect the order of items in the hash.
If you need to keep iterators over a long period of time, we recommend
that you use QMap rather than QHash.
that you use QMultiMap rather than QMultiHash.
\warning Iterators on implicitly shared containers do not work
exactly like STL-iterators. You should avoid copying a container
while iterators are active on that container. For more information,
read \l{Implicit sharing iterator problem}.
\sa QMultiHash::iterator
\sa QMultiHash::iterator, QMultiHash::key_iterator, QMultiHash::const_key_value_iterator
*/
/*! \fn template <class Key, class T> QMultiHash<Key, T>::const_iterator::const_iterator()

View File

@ -783,12 +783,12 @@
/*! \typedef QMap::Iterator
Qt-style synonym for QMap<Key, T>::iterator.
Qt-style synonym for QMap::iterator.
*/
/*! \typedef QMap::ConstIterator
Qt-style synonym for QMap<Key, T>::const_iterator.
Qt-style synonym for QMap::const_iterator.
*/
/*! \typedef QMap::difference_type
@ -837,12 +837,6 @@
\inmodule QtCore
\brief The QMap::iterator class provides an STL-style non-const iterator for QMap.
QMap features both \l{STL-style iterators} and \l{Java-style
iterators}. The STL-style iterators are more low-level and more
cumbersome to use; on the other hand, they are slightly faster
and, for developers who already know STL, have the advantage of
familiarity.
QMap\<Key, T\>::iterator allows you to iterate over a QMap
and to modify the value (but not the key) stored under
a particular key. If you want to iterate over a const QMap, you
@ -862,31 +856,15 @@
Unlike QHash, which stores its items in an arbitrary order, QMap
stores its items ordered by key.
Let's see a few examples of things we can do with a
QMap::iterator that we cannot do with a QMap::const_iterator.
Here's an example that increments every value stored in the QMap
by 2:
\snippet code/src_corelib_tools_qmap.cpp 19
Here's an example that removes all the items whose key is a
string that starts with an underscore character:
\snippet code/src_corelib_tools_qmap.cpp 20
The call to QMap::erase() removes the item pointed to by the
iterator from the map, and returns an iterator to the next item.
Here's another way of removing an item while iterating:
To remove elements from a QMap you can use erase_if(QMap\<Key, T\> &map, Predicate pred):
\snippet code/src_corelib_tools_qmap.cpp 21
It might be tempting to write code like this:
\snippet code/src_corelib_tools_qmap.cpp 22
However, this will potentially crash in \c{++i}, because \c i is
a dangling iterator after the call to erase().
Multiple iterators can be used on the same map. If you add items
to the map, existing iterators will remain valid. If you remove
items from the map, iterators that point to the removed items
@ -897,7 +875,7 @@
while iterators are active on that container. For more information,
read \l{Implicit sharing iterator problem}.
\sa QMap::const_iterator, QMap::key_iterator, QMutableMapIterator
\sa QMap::const_iterator, QMap::key_iterator, QMap::key_value_iterator
*/
/*! \typedef QMap::iterator::difference_type
@ -1057,12 +1035,6 @@
\inmodule QtCore
\brief The QMap::const_iterator class provides an STL-style const iterator for QMap.
QMap features both \l{STL-style iterators} and \l{Java-style
iterators}. The STL-style iterators are more low-level and more
cumbersome to use; on the other hand, they are slightly faster
and, for developers who already know STL, have the advantage of
familiarity.
QMap\<Key, T\>::const_iterator allows you to iterate over a QMap.
If you want to modify the QMap as you iterate
over it, you must use QMap::iterator instead. It is generally
@ -1073,12 +1045,20 @@
The default QMap::const_iterator constructor creates an
uninitialized iterator. You must initialize it using a QMap
function like QMap::constBegin(), QMap::constEnd(), or
QMap::find() before you can start iterating. Here's a typical
function like QMap::cbegin(), QMap::cend(), or
QMap::constFind() before you can start iterating. Here's a typical
loop that prints all the (key, value) pairs stored in a map:
\snippet code/src_corelib_tools_qmap.cpp 24
Here's an example that removes all the items whose value is greater than 10:
\snippet code/src_corelib_tools_qmap.cpp 20
And here the same behavior with erase_if()
\snippet code/src_corelib_tools_qmap.cpp 21
Unlike QHash, which stores its items in an arbitrary order, QMap
stores its items ordered by key.
@ -1092,7 +1072,7 @@
while iterators are active on that container. For more information,
read \l{Implicit sharing iterator problem}.
\sa QMap::iterator, QMap::key_iterator, QMapIterator
\sa QMap::iterator, QMap::key_iterator, QMap::const_key_value_iterator
*/
/*! \typedef QMap::const_iterator::difference_type

View File

@ -891,12 +891,12 @@
/*! \typedef QMultiMap::Iterator
Qt-style synonym for QMultiMap<Key, T>::iterator.
Qt-style synonym for QMultiMap::iterator.
*/
/*! \typedef QMultiMap::ConstIterator
Qt-style synonym for QMultiMap<Key, T>::const_iterator.
Qt-style synonym for QMultiMap::const_iterator.
*/
/*! \typedef QMultiMap::difference_type
@ -981,12 +981,6 @@
\inmodule QtCore
\brief The QMultiMap::iterator class provides an STL-style non-const iterator for QMultiMap.
QMultiMap features both \l{STL-style iterators} and \l{Java-style
iterators}. The STL-style iterators are more low-level and more
cumbersome to use; on the other hand, they are slightly faster
and, for developers who already know STL, have the advantage of
familiarity.
QMultiMap\<Key, T\>::iterator allows you to iterate over a QMultiMap
and to modify the value (but not the key) stored under
a particular key. If you want to iterate over a const QMultiMap, you
@ -1001,38 +995,20 @@
start iterating. Here's a typical loop that prints all the (key,
value) pairs stored in a map:
\snippet code/src_corelib_tools_qmultimap.cpp 18
Unlike QMultiHash, which stores its items in an arbitrary order, QMultiMap
stores its items ordered by key. Items that share the same key
will appear consecutively,
from the most recently to the least recently inserted value.
Let's see a few examples of things we can do with a
QMultiMap::iterator that we cannot do with a QMultiMap::const_iterator.
Here's an example that increments every value stored in the QMultiMap
by 2:
\snippet code/src_corelib_tools_qmultimap.cpp 19
Here's an example that removes all the items whose key is a
string that starts with an underscore character:
\snippet code/src_corelib_tools_qmultimap.cpp 20
The call to QMultiMap::erase() removes the item pointed to by the
iterator from the map, and returns an iterator to the next item.
Here's another way of removing an item while iterating:
To remove elements from a QMultiMap you can use erase_if(QMultiMap\<Key, T\> &map, Predicate pred):
\snippet code/src_corelib_tools_qmultimap.cpp 21
It might be tempting to write code like this:
\snippet code/src_corelib_tools_qmultimap.cpp 22
However, this will potentially crash in \c{++i}, because \c i is
a dangling iterator after the call to erase().
Multiple iterators can be used on the same map. If you add items
to the map, existing iterators will remain valid. If you remove
items from the map, iterators that point to the removed items
@ -1043,7 +1019,7 @@
while iterators are active on that container. For more information,
read \l{Implicit sharing iterator problem}.
\sa QMultiMap::const_iterator, QMultiMap::key_iterator, QMutableMultiMapIterator
\sa QMultiMap::const_iterator, QMultiMap::key_iterator, QMultiMap::key_value_iterator
*/
/*! \typedef QMultiMap::iterator::difference_type
@ -1204,12 +1180,6 @@
\inmodule QtCore
\brief The QMultiMap::const_iterator class provides an STL-style const iterator for QMultiMap.
QMultiMap features both \l{STL-style iterators} and \l{Java-style
iterators}. The STL-style iterators are more low-level and more
cumbersome to use; on the other hand, they are slightly faster
and, for developers who already know STL, have the advantage of
familiarity.
QMultiMap\<Key, T\>::const_iterator allows you to iterate over a QMultiMap.
If you want to modify the QMultiMap as you iterate
over it, you must use QMultiMap::iterator instead. It is generally
@ -1220,12 +1190,16 @@
The default QMultiMap::const_iterator constructor creates an
uninitialized iterator. You must initialize it using a QMultiMap
function like QMultiMap::constBegin(), QMultiMap::constEnd(), or
QMultiMap::find() before you can start iterating. Here's a typical
function like QMultiMap::cbegin(), QMultiMap::cend(), or
QMultiMap::constFind() before you can start iterating. Here's a typical
loop that prints all the (key, value) pairs stored in a map:
\snippet code/src_corelib_tools_qmultimap.cpp 24
Here's an example that removes all the items whose value is greater than 10:
\snippet code/src_corelib_tools_qmultimap.cpp 20
Unlike QMultiHash, which stores its items in an arbitrary order, QMultiMap
stores its items ordered by key. Items that share the same key
will appear consecutively,
@ -1241,7 +1215,7 @@
while iterators are active on that container. For more information,
read \l{Implicit sharing iterator problem}.
\sa QMultiMap::iterator, QMultiMap::key_iterator, QMultiMapIterator
\sa QMultiMap::iterator, QMultiMap::key_iterator, QMultiMap::const_key_value_iterator
*/
/*! \typedef QMultiMap::const_iterator::difference_type