key_iterator Classclass QHash::key_iterator
This class was introduced in Qt 5.6.
|bool||operator!=(key_iterator other) const|
|const Key &||operator*() const|
|const Key *||operator->() const|
|bool||operator==(key_iterator other) const|
However, to have interoperability between QHash's keys and STL-style algorithms we need an iterator that dereferences to a key instead of a value. With QHash::key_iterator we can apply an algorithm to a range of keys without having to call QHash::keys(), which is inefficient as it costs one QHash iteration and memory allocation to create a temporary QList.
// 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());
QHash::key_iterator is const, it's not possible to modify the key.
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 Implicit sharing iterator problem.
Member Function Documentation
const_iterator key_iterator::base() const
true if other points to a different item than this iterator; otherwise returns
See also operator==().
Returns the current item's key.
The prefix ++ operator (
++i) advances the iterator to the next item in the hash and returns an iterator to the new current item.
Calling this function on QHash::keyEnd() leads to undefined results.
This is an overloaded function.
The postfix ++ operator (
i++) advances the iterator to the next item in the hash and returns an iterator to the previous item.
Returns a pointer to the current item's key.
true if other points to the same item as this iterator; otherwise returns
See also operator!=().
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.