summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp
diff options
context:
space:
mode:
authorSérgio Martins <sergio.martins@kdab.com>2015-06-22 17:21:34 +0100
committerSérgio Martins <sergio.martins@kdab.com>2015-07-28 19:11:17 +0000
commit4bced6e7a6928137bb7ac965d2b07223880a3e39 (patch)
tree4ac4b404574c671286ebae8fdad3e5d0b0b60120 /src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp
parent07f27fcf6d4f046bbea6c3155efa78128e164da4 (diff)
Introduce QHash key iterators
So we can have interoperability with algorithms. Motivated by inefficient code like qDeleteAll(hash.keys()) [ChangeLog][QtCore][QHash] Added key iterators, accessible through keyBegin() and keyEnd(). Change-Id: I1f9db8a7a4294e1556cbb50b8fe5ebdcf0dc29a2 Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Diffstat (limited to 'src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp')
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp19
1 files changed, 19 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 43d64fc08e..0ac7cb5769 100644
--- a/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp
@@ -298,6 +298,25 @@ while (i != hash.end() && i.key() == "plenty") {
}
//! [26]
+//! [27]
+for (QHash<int, QString>::const_iterator it = hash.cbegin(), end = hash.cend(); it != end; ++it) {
+ cout << "The key: " << it.key() << endl
+ cout << "The value: " << it.value() << endl;
+ cout << "Also the value: " << (*it) << endl;
+}
+//! [27]
+
+//! [28]
+// Inefficient, keys() is expensive
+QList<int> keys = hash.keys();
+int numPrimes = std::count_if(keys.cbegin(), keys.cend(), isPrimeNumber);
+qDeleteAll(hash2.keys());
+
+// Efficient, no memory allocation needed
+int numPrimes = std::count_if(hash.keyBegin(), hash.keyEnd(), isPrimeNumber);
+qDeleteAll(hash2.keyBegin(), hash2.keyEnd());
+//! [28]
+
//! [qhashbits]
inline uint qHash(const std::vector<int> &key, uint seed = 0)
{