key_iterator Class

(QHash::key_iterator)

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

Header: #include <key_iterator>
qmake: QT += core
Since: Qt 5.6

Public Functions

key_iterator()
key_iterator(const_iterator o)
const_iterator base() const
bool operator!=(key_iterator o) const
const Key &operator*() const
key_iterator &operator++()
key_iterator operator++(int)
key_iterator &operator--()
key_iterator operator--(int)
const Key *operator->() const
bool operator==(key_iterator o) const

Detailed Description

The QHash::key_iterator class provides an STL-style const iterator for QHash and QMultiHash keys.

QHash::key_iterator is essentially the same as QHash::const_iterator with the difference that operator*() and operator->() return a key instead of a value.

For most uses QHash::iterator and QHash::const_iterator should be used, you can easily access the key by calling QHash::iterator::key():


  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;
  }

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.

The default QHash::key_iterator constructor creates an uninitialized iterator. You must initialize it using a QHash function like QHash::keyBegin() or QHash::keyEnd().

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 and QHash::iterator.

Member Function Documentation

key_iterator::key_iterator()

Default constructs an instance of key_iterator.

key_iterator::key_iterator(const_iterator o)

Default constructs an instance of key_iterator.

const_iterator key_iterator::base() const

bool key_iterator::operator!=(key_iterator o) const

const Key &key_iterator::operator*() const

key_iterator &key_iterator::operator++()

key_iterator key_iterator::operator++(int)

key_iterator &key_iterator::operator--()

key_iterator key_iterator::operator--(int)

const Key *key_iterator::operator->() const

bool key_iterator::operator==(key_iterator o) const