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> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Since | 5.10 |
List of all members, including inherited members
Public Types
| typedef | iterator_category |
| typedef | difference_type |
| typedef | value_type |
| typedef | reference |
Public Functions
| QKeyValueIterator() | |
| QKeyValueIterator(Iterator o) | |
| value_type | operator*() const |
| pointer | operator->() const |
| QKeyValueIterator & | operator++() |
| QKeyValueIterator | operator++(int) |
| QKeyValueIterator & | operator--() |
| QKeyValueIterator | operator--(int) |
| Iterator | base() 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;
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.
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.