iterator Class

class QHash::iterator

The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash. More...

Public Functions

iterator()
const Key &key() const
T &value() const
bool operator!=(const iterator &other) const
bool operator!=(const const_iterator &other) const
T &operator*() const
iterator &operator++()
iterator operator++(int)
T *operator->() const
bool operator==(const iterator &other) const
bool operator==(const const_iterator &other) const

Detailed Description

QHash features both STL-style iterators and Java-style iterators. The STL-style iterators are more low-level and more cumbersome to use; on the other hand, they are slightly faster and, for developers who already know STL, have the advantage of familiarity.

QHash<Key, T>::iterator allows you to iterate over a QHash (or QMultiHash) and to modify the value (but not the key) associated with a particular key. If you want to iterate over a const QHash, you should use QHash::const_iterator. It is generally good practice to use QHash::const_iterator on a non-const QHash as well, unless you need to change the QHash through the iterator. Const iterators are slightly faster, and can improve code readability.

The default QHash::iterator constructor creates an uninitialized iterator. You must initialize it using a QHash function like QHash::begin(), QHash::end(), or QHash::find() before you can start iterating. Here's a typical loop that prints all the (key, value) pairs stored in a hash:

QHash<QString, int> hash;
hash.insert("January", 1);
hash.insert("February", 2);
...
hash.insert("December", 12);

QHash<QString, int>::iterator i;
for (i = hash.begin(); i != hash.end(); ++i)
    cout << i.key() << ": " << i.value() << Qt::endl;

Unlike QMap, which orders its items by key, QHash stores its items in an arbitrary order.

Let's see a few examples of things we can do with a QHash::iterator that we cannot do with a QHash::const_iterator. Here's an example that increments every value stored in the QHash by 2:

QHash<QString, int>::iterator i;
for (i = hash.begin(); i != hash.end(); ++i)
    i.value() += 2;

Here's an example that removes all the items whose key is a string that starts with an underscore character:

QHash<QString, int>::iterator i = hash.begin();
while (i != hash.end()) {
    if (i.key().startsWith('_'))
        i = hash.erase(i);
    else
        ++i;
}

The call to QHash::erase() removes the item pointed to by the iterator from the hash, and returns an iterator to the next item. Here's another way of removing an item while iterating:

QHash<QString, int>::iterator i = hash.begin();
while (i != hash.end()) {
    QHash<QString, int>::iterator prev = i;
    ++i;
    if (prev.key().startsWith('_'))
        hash.erase(prev);
}

It might be tempting to write code like this:

// WRONG
while (i != hash.end()) {
    if (i.key().startsWith('_'))
        hash.erase(i);
    ++i;
}

However, this will potentially crash in ++i, because i is a dangling iterator after the call to erase().

Multiple iterators can be used on the same hash. However, be aware that any modification performed directly on the QHash has the potential of dramatically changing the order in which the items are stored in the hash, as they might cause QHash to rehash its internal data structure. There is one notable exception: QHash::erase(). This function can safely be called while iterating, and won't affect the order of items in the hash. If you need to keep iterators over a long period of time, we recommend that you use QMap rather than QHash.

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.

See also QHash::const_iterator, QHash::key_iterator, and QMutableHashIterator.

Member Function Documentation

bool iterator::operator!=(const const_iterator &other) const

bool iterator::operator!=(const iterator &other) const

Returns true if other points to a different item than this iterator; otherwise returns false.

See also operator==().

bool iterator::operator==(const const_iterator &other) const

bool iterator::operator==(const iterator &other) const

Returns true if other points to the same item as this iterator; otherwise returns false.

See also operator!=().

iterator::iterator()

Constructs an uninitialized iterator.

Functions like key(), value(), and operator++() must not be called on an uninitialized iterator. Use operator=() to assign a value to it before using it.

See also QHash::begin() and QHash::end().

const Key &iterator::key() const

Returns the current item's key as a const reference.

There is no direct way of changing an item's key through an iterator, although it can be done by calling QHash::erase() followed by QHash::insert().

See also value().

T &iterator::value() const

Returns a modifiable reference to the current item's value.

You can change the value of an item by using value() on the left side of an assignment, for example:

if (i.key() == "Hello")
    i.value() = "Bonjour";

See also key() and operator*().

T &iterator::operator*() const

Returns a modifiable reference to the current item's value.

Same as value().

See also key().

iterator &iterator::operator++()

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::end() leads to undefined results.

See also operator--().

iterator iterator::operator++(int)

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 previously current item.

T *iterator::operator->() const

Returns a pointer to the current item's value.

See also value().

© 2023 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.