Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QHash< Key, T >::key_iterator Class Reference

\inmodule QtCore More...

#include <qhash.h>

+ Collaboration diagram for QHash< Key, T >::key_iterator:

Public Types

typedef const_iterator::iterator_category iterator_category
 
typedef qptrdiff difference_type
 
typedef Key value_type
 
typedef const Keypointer
 
typedef const Keyreference
 

Public Member Functions

 key_iterator () noexcept=default
 
 key_iterator (const_iterator o) noexcept
 
const Keyoperator* () const noexcept
 Returns the current item's key.
 
const Keyoperator-> () const noexcept
 Returns a pointer to the current item's key.
 
bool operator== (key_iterator o) const noexcept
 Returns true if other points to the same item as this iterator; otherwise returns false.
 
bool operator!= (key_iterator o) const noexcept
 Returns true if other points to a different item than this iterator; otherwise returns false.
 
key_iteratoroperator++ () noexcept
 The prefix ++ operator ({++i}) advances the iterator to the next item in the hash and returns an iterator to the new current item.
 
key_iterator operator++ (int) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.The postfix ++ operator ({i++}) advances the iterator to the next item in the hash and returns an iterator to the previous item.
 
const_iterator base () const noexcept
 Returns the underlying const_iterator this key_iterator is based on.
 

Detailed Description

template<typename Key, typename T>
class QHash< Key, T >::key_iterator

\inmodule QtCore

Since
5.6

The QHash::key_iterator class provides an STL-style const iterator for QHash 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 (auto it = hash.cbegin(), end = hash.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 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 \l{Implicit sharing iterator problem}.
See also
QHash::const_iterator, QHash::iterator

Definition at line 1209 of file qhash.h.

Member Typedef Documentation

◆ difference_type

template<typename Key , typename T >
qptrdiff QHash< Key, T >::key_iterator::difference_type

Definition at line 1215 of file qhash.h.

◆ iterator_category

template<typename Key , typename T >
const_iterator::iterator_category QHash< Key, T >::key_iterator::iterator_category

Definition at line 1214 of file qhash.h.

◆ pointer

template<typename Key , typename T >
const Key* QHash< Key, T >::key_iterator::pointer

Definition at line 1217 of file qhash.h.

◆ reference

template<typename Key , typename T >
const Key& QHash< Key, T >::key_iterator::reference

Definition at line 1218 of file qhash.h.

◆ value_type

template<typename Key , typename T >
Key QHash< Key, T >::key_iterator::value_type

Definition at line 1216 of file qhash.h.

Constructor & Destructor Documentation

◆ key_iterator() [1/2]

template<typename Key , typename T >
QHash< Key, T >::key_iterator::key_iterator ( )
defaultnoexcept

◆ key_iterator() [2/2]

template<typename Key , typename T >
QHash< Key, T >::key_iterator::key_iterator ( const_iterator o)
inlineexplicitnoexcept

Definition at line 1221 of file qhash.h.

Member Function Documentation

◆ base()

template<typename Key , typename T >
const_iterator QHash< Key, T >::key_iterator::base ( ) const
inlinenoexcept

Returns the underlying const_iterator this key_iterator is based on.

Definition at line 1230 of file qhash.h.

◆ operator!=()

template<typename Key , typename T >
bool QHash< Key, T >::key_iterator::operator!= ( key_iterator other) const
inlinenoexcept

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

See also
operator==()

Definition at line 1226 of file qhash.h.

◆ operator*()

template<typename Key , typename T >
const Key & QHash< Key, T >::key_iterator::operator* ( ) const
inlinenoexcept

Returns the current item's key.

Definition at line 1223 of file qhash.h.

◆ operator++() [1/2]

template<typename Key , typename T >
key_iterator & QHash< Key, T >::key_iterator::operator++ ( )
inlinenoexcept

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 QHash::keyEnd() leads to undefined results.

Definition at line 1228 of file qhash.h.

◆ operator++() [2/2]

template<typename Key , typename T >
key_iterator QHash< Key, T >::key_iterator::operator++ ( int )
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.The postfix ++ operator ({i++}) advances the iterator to the next item in the hash and returns an iterator to the previous item.

Definition at line 1229 of file qhash.h.

◆ operator->()

template<typename Key , typename T >
const Key * QHash< Key, T >::key_iterator::operator-> ( ) const
inlinenoexcept

Returns a pointer to the current item's key.

Definition at line 1224 of file qhash.h.

◆ operator==()

template<typename Key , typename T >
bool QHash< Key, T >::key_iterator::operator== ( key_iterator other) const
inlinenoexcept

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

See also
operator!=()

Definition at line 1225 of file qhash.h.


The documentation for this class was generated from the following files: