6.0.0-rc4Source

QKeyValueIterator Class

template <typename Key, typename T, typename Iterator> class QKeyValueIterator

The QKeyValueIterator class is an iterator over the key and value pairs of an associative container. More…

Header#include <QKeyValueIterator>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
Since5.10

List of all members, including inherited members

Public Types

Public Functions

QKeyValueIterator()
QKeyValueIterator(Iterator o)
value_typeoperator*() const
pointeroperator->() const
QKeyValueIterator &operator++()
QKeyValueIteratoroperator++(int)
QKeyValueIterator &operator--()
QKeyValueIteratoroperator--(int)
Iteratorbase() const

Detailed Description

keyValueBegin() and keyValueEnd() of QMap, QMultiMap, QHash and QMultiHash return this iterator. Dereferencing it gives a std::pair of the key and the value, so standard algorithms see both:

QMap<QString, int> ages{{u"ada"_s, 36}, {u"alan"_s, 41}};
auto oldest = std::max_element(ages.keyValueBegin(), ages.keyValueEnd(),
                               [](const auto &a, const auto &b) { return a.second < b.second; });

asKeyValueRange() wraps the same iterators in a range for a range-based for loop with structured bindings:

for (auto [name, age] : ages.asKeyValueRange())
    qDebug() << name << age;

See also QMap and QHash.

Differences from Qt

The iterator is a forward and backward iterator for every container, whatever the category of the container's own iterator. operator->() returns a proxy that holds the pair.

Member Type Documentation

typedef QKeyValueIterator::difference_type

The type of the distance between two iterators.

typedef QKeyValueIterator::iterator_category

The iterator category, std::bidirectional_iterator_tag.

typedef QKeyValueIterator::reference

The type returned by operator*(), the same as value_type.

typedef QKeyValueIterator::value_type

The type of an item, std::pair<Key, T>.

Member Function Documentation

QKeyValueIterator::QKeyValueIterator()

Constructs a default iterator, which must not be dereferenced.

[explicit, constexpr] QKeyValueIterator::QKeyValueIterator(Iterator o)

Constructs an iterator over the item that o refers to.

Iterator QKeyValueIterator::base() const

Returns the iterator of the container that this iterator wraps.

value_type QKeyValueIterator::operator*() const

Returns the key and the value of the current item.

QKeyValueIterator & QKeyValueIterator::operator++()

Moves the iterator to the next item and returns it.

QKeyValueIterator QKeyValueIterator::operator++(int)

This is an overloaded function.

Moves the iterator to the next item and returns the previous position.

QKeyValueIterator & QKeyValueIterator::operator--()

Moves the iterator to the previous item and returns it.

QKeyValueIterator QKeyValueIterator::operator--(int)

This is an overloaded function.

Moves the iterator to the previous item and returns the previous position.

pointer QKeyValueIterator::operator->() const

Returns a proxy for accessing the key and the value of the current item through ->first and ->second.