diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2022-03-01 04:24:38 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2022-03-04 00:21:44 +0100 |
commit | 0deff80eabd577f3514d255e32f74502c914dfe1 (patch) | |
tree | 353014233bbb87123679ac985de18daee3834341 /src/corelib/doc/snippets | |
parent | dd5fc20e902187356538d1c993d9ece729f6f505 (diff) |
Associative containers: add a way to obtain a key/value range
Our associative containers' iterator's value_type isn't a destructurable
type (yielding key/value). This means that something like
for (auto [k, v] : map)
doesn't even compile -- one can only "directly" iterate on the
values. For quite some time we've had QKeyValueIterator to allow
key/value iteration, but then one had to resort to a "traditional" for
loop:
for (auto i = map.keyValueBegin(), e = keyValueEnd(); i!=e; ++i)
This can be easily packaged in an adaptor class, which is what this
commmit does, thereby offering a C++17-compatible way to obtain
key/value iteration over associative containers.
Something possibly peculiar is the fact that the range so obtained is
a range of pairs of references -- not a range of references to pairs.
But that's easily explained by the fact that we have no pairs to build
references to; hence,
for (auto &[k, v] : map.asKeyValueRange())
doesn't compile (lvalue reference doesn't bind to prvalue pair).
Instead, both of these compile:
for (auto [k, v] : map.asKeyValueRange())
for (auto &&[k, v] : map.asKeyValueRange())
and in *both* cases one gets references to the keys/values in the map.
If the map is non-const, the reference to the value is mutable.
Last but not least, implement pinning for rvalue containers.
[ChangeLog][QtCore][QMap] Added asKeyValueRange().
[ChangeLog][QtCore][QMultiMap] Added asKeyValueRange().
[ChangeLog][QtCore][QHash] Added asKeyValueRange().
[ChangeLog][QtCore][QMultiHash] Added asKeyValueRange().
Task-number: QTBUG-4615
Change-Id: Ic8506bff38b2f753494b21ab76f52e05c06ffc8b
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/doc/snippets')
3 files changed, 52 insertions, 0 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 ee9f88fc65..844c5f1b7c 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp @@ -360,3 +360,29 @@ template <> struct hash<K> }; } //! [33] + +//! [34] +QHash<QString, int> hash; +hash.insert("January", 1); +hash.insert("February", 2); +// ... +hash.insert("December", 12); + +for (auto [key, value] : hash.asKeyValueRange()) { + cout << key << ": " << value << Qt::endl; + --value; // convert to JS month indexing +} +//! [34] + +//! [35] +QMultiHash<QString, int> hash; +hash.insert("January", 1); +hash.insert("February", 2); +// ... +hash.insert("December", 12); + +for (auto [key, value] : hash.asKeyValueRange()) { + cout << key << ": " << value << Qt::endl; + --value; // convert to JS month indexing +} +//! [35] 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..a0e280e759 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qmap.cpp @@ -340,3 +340,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 << key << ": " << value << Qt::endl; + --value; // convert to JS month indexing +} +//! [28] 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..bd188c3fb4 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp @@ -322,3 +322,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 << key << ": " << value << Qt::endl; + --value; // convert to JS month indexing +} +//! [26] |