From 4bced6e7a6928137bb7ac965d2b07223880a3e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Martins?= Date: Mon, 22 Jun 2015 17:21:34 +0100 Subject: 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 --- .../doc/snippets/code/src_corelib_tools_qhash.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/corelib/doc/snippets/code/src_corelib_tools_qhash.cpp') 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::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 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 &key, uint seed = 0) { -- cgit v1.2.3