key_iterator Class▲
-
Header: key_iterator
-
Since: Qt 5.6
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
Detailed Description▲
QMultiMap::key_iterator is essentially the same as QMultiMap::const_iterator with the difference that operator*() and operator->() return a key instead of a value.
For most uses QMultiMap::iterator and QMultiMap::const_iterator should be used, you can easily access the key by calling QMultiMap::iterator::key():
for
(QMultiMap&
lt;int
, QString&
gt;::
const_iterator it =
multimap.cbegin(), end =
multimap.cend(); it !=
end; ++
it) {
cout &
lt;&
lt; "The key: "
&
lt;&
lt; it.key() &
lt;&
lt; Qt::
endl
cout &
lt;&
lt; "The value: "
&
lt;&
lt; it.value() &
lt;&
lt; Qt::
endl;
cout &
lt;&
lt; "Also the value: "
&
lt;&
lt; (*
it) &
lt;&
lt; Qt::
endl;
}
However, to have interoperability between QMultiMap's keys and STL-style algorithms we need an iterator that dereferences to a key instead of a value. With QMultiMap::key_iterator we can apply an algorithm to a range of keys without having to call QMultiMap::keys(), which is inefficient as it costs one QMultiMap iteration and memory allocation to create a temporary QList.
// Inefficient, keys() is expensive
QList&
lt;int
&
gt; keys =
multimap.keys();
int
numPrimes =
std::
count_if(multimap.cbegin(), multimap.cend(), isPrimeNumber);
qDeleteAll(multimap2.keys());
// Efficient, no memory allocation needed
int
numPrimes =
std::
count_if(multimap.keyBegin(), multimap.keyEnd(), isPrimeNumber);
qDeleteAll(multimap2.keyBegin(), multimap2.keyEnd());
QMultiMap::key_iterator is const, it's not possible to modify the key.
The default QMultiMap::key_iterator constructor creates an uninitialized iterator. You must initialize it using a QMultiMap function like QMultiMap::keyBegin() or QMultiMap::keyEnd().
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▲
See also QMultiMap::const_iterator, QMultiMap::iterator
Member Function Documentation▲
const_iterator key_iterator::base() const▲
Returns the underlying const_iterator this key_iterator is based on.
bool key_iterator::operator!=(key_iterator other) const▲
Returns true if other points to a different item than this iterator; otherwise returns false.
See Also▲
See also operator==()
const Key &key_iterator::operator*() const▲
Returns the current item's key.
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 QMultiMap::keyEnd() leads to undefined results.
See Also▲
See also operator--()
key_iterator key_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 previous item.
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 QMultiMap::keyBegin() leads to undefined results.
See Also▲
See also operator++()
key_iterator key_iterator::operator--(int)▲
This is an overloaded function.
The postfix – operator (i--) makes the preceding item current and returns an iterator pointing to the previous item.
const Key *key_iterator::operator->() const▲
Returns a pointer to the current item's key.
bool key_iterator::operator==(key_iterator other) const▲
Returns true if other points to the same item as this iterator; otherwise returns false.
See Also▲
See also operator!=()