- Qt 6.10
- Qt Core
- C++ Classes
- key_iterator
- Qt 6.10.2 Reference Documentation

# QMap::key_iterator Class

class QMap::key_iterator

The QMap::key_iterator class provides an STL-style const iterator for QMap keys. More...

- List of all members, including inherited members

## Public Functions

QMap<Key, T>::const_iterator | base() const
bool | operator!=(QMap<Key, T>::key_iterator other) const
const Key & | operator*() const
QMap<Key, T>::key_iterator & | operator++()
QMap<Key, T>::key_iterator | operator++(int)
QMap<Key, T>::key_iterator & | operator--()
QMap<Key, T>::key_iterator | operator--(int)
const Key * | operator->() const
bool | operator==(QMap<Key, T>::key_iterator other) const

## Detailed Description

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

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

     for (QMap<int, QString>::const_iterator it = map.cbegin(), end = map.cend(); it != end; ++it) {
         cout << "The key: " << it.key() << endl;
         cout << "The value: " << qPrintable(it.value()) << endl;
         cout << "Also the value: " << qPrintable(*it) << endl;
     }

However, to have interoperability between QMap's keys and STL-style algorithms we need an iterator that dereferences to a key instead of a value. With QMap::key_iterator we can apply an algorithm to a range of keys without having to call QMap::keys(), which is inefficient as it costs one QMap iteration and memory allocation to create a temporary QList.

     // Inefficient, keys() is expensive
     QList<int> keys = map.keys();
     int numPrimes = std::count_if(map.cbegin(), map.cend(), isPrimeNumber);
     qDeleteAll(map2.keys());

     // Efficient, no memory allocation needed
     int numPrimes = std::count_if(map.keyBegin(), map.keyEnd(), isPrimeNumber);
     qDeleteAll(map2.keyBegin(), map2.keyEnd());

QMap::key_iterator is const, it's not possible to modify the key.

The default QMap::key_iterator constructor creates an uninitialized iterator. You must initialize it using a QMap function like QMap::keyBegin() or QMap::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 QMap::const_iterator and QMap::iterator.

## Member Function Documentation

### QMap<Key, T>::const_iterator key_iterator::base() const

Returns the underlying const_iterator this key_iterator is based on.

### bool key_iterator::operator!=(QMap<Key, T>::key_iterator other) const

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

See also operator==().

### const Key &key_iterator::operator*() const

Returns the current item's key.

### QMap<Key, T>::key_iterator &key_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 QMap::keyEnd() leads to undefined results.

See also operator--().

### QMap<Key, T>::key_iterator key_iterator::operator++(int)

The postfix `++` operator (`i++`) advances the iterator to the next item in the hash and returns an iterator to the previous item.

This is an overloaded function.

### QMap<Key, T>::key_iterator &key_iterator::operator--()

The prefix `--` operator (`--i`) makes the preceding item current and returns an iterator pointing to the new current item.

Calling this function on QMap::keyBegin() leads to undefined results.

See also operator++().

### QMap<Key, T>::key_iterator key_iterator::operator--(int)

The postfix `--` operator (`i--`) makes the preceding item current and returns an iterator pointing to the previous item.

This is an overloaded function.

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

Returns a pointer to the current item's key.

### bool key_iterator::operator==(QMap<Key, T>::key_iterator other) const

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

See also operator!=().