diff options
| author | Paul Wicking <paul.wicking@qt.io> | 2025-09-01 21:54:28 +0200 |
|---|---|---|
| committer | Paul Wicking <paul.wicking@qt.io> | 2025-09-09 22:08:21 +0000 |
| commit | c068da1c7ccffdb41d5bfdc5dad8c276ed5d75c4 (patch) | |
| tree | fdb4da30e244abc5ec46c5f88b318418003ec718 /src/corelib/doc/snippets | |
| parent | c51b2b1bfaee1ed674e6d860196f6d6db4d50ec8 (diff) | |
Doc: Modernize QHash iterator documentation
Promote STL-style iterators as the primary way to iterate over QHash.
Replace the detailed explanation with direct guidance to use
QHash::asKeyValueRange, and move Java-style iterators to a brief
compatibility note.
Update snippets to:
- Show C++17 structured bindings with
std::as_const(hash).asKeyValueRange() (add <utility> header
std::as_const).
- Add STL-style iterator example for manual control.
- Add example for modifying values in place.
This makes the docs clearer, const-correct, and aligned with modern C++
usage.
Fixes: QTBUG-139662
Pick-to: 6.10.0 6.10 6.9 6.8
Change-Id: Ifb9698b93ca53c3c6a7c82b0f1d393105cd62f35
Reviewed-by: Topi Reiniƶ <topi.reinio@qt.io>
Diffstat (limited to 'src/corelib/doc/snippets')
| -rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp index ec9e704fb17..eea61b1b410 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp @@ -6,6 +6,8 @@ #include <iostream> #include <QDate> +#include <utility> + using namespace std; void examples() @@ -61,12 +63,26 @@ void examples() { //! [8] - for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i) - cout << qPrintable(i.key()) << ": " << i.value() << endl; + for (const auto &[key, value] : std::as_const(hash).asKeyValueRange()) + cout << qPrintable(key) << ": " << value << endl; //! [8] } { + //! [qhash-iterator-stl-style] + for (auto it = hash.cbegin(); it != hash.cend(); ++it) + cout << qPrintable(it.key()) << ": " << it.value() << endl; + //! [qhash-iterator-stl-style] + } + + { + //! [qhash-iterator-modify-values] + for (auto it = hash.begin(); it != hash.end(); ++it) + it.value() += 1; + //! [qhash-iterator-modify-values] + } + + { //! [9] hash.insert("plenty", 100); hash.insert("plenty", 2000); |
